Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split strings and save comma int python

I have the following string

c='a,b,c,"d,e",f,g'  

and I want to get

b=['a','b','c','d,e','f','g']

so

b[3]=='d,e'

any ideas? the problem with c.split(',') is that it splits also 'd,e'

[I have see an answer here for C++, that of course didn't help me]

Many Thanks

like image 887
user552231 Avatar asked Oct 04 '12 11:10

user552231


People also ask

How do you separate a string or int with a comma in Python?

You can use the Python string split() function to split a string (by a delimiter) into a list of strings. To split a string by comma in Python, pass the comma character "," as a delimiter to the split() function. It returns a list of strings resulting from splitting the original string on the occurrences of "," .

How do you store Comma Separated Values in Python?

Given an input string that is comma-separated instead of space. The task is to store this input string in a list or variables. This can be achieved in Python using two ways: Using List comprehension and split()

How do you split a comma-separated string into a list in Python?

Use str. split() to convert a comma-separated string to a list. Call str. split(sep) with "," as sep to convert a comma-separated string into a list.

How do you split a space with integers in Python?

Use input(), map() and split() function to take space-separated integer input in Python 3. You have to use list() to convert the map to a list.


1 Answers

You could use the CSV module if c should indeed be the below...

import csv
c = 'a,b,c,"d,e",f,g'
print next(csv.reader([c]))
# ['a', 'b', 'c', 'd,e', 'f', 'g']
like image 145
Jon Clements Avatar answered Oct 16 '22 04:10

Jon Clements