How to convert a string with comma-delimited items to a list in Python?
How do you convert a string into a list?
Say the string is like
text = "a,b,c". After the conversion,
text == ['a', 'b', 'c'] and hopefully
text == 'a',
text == 'b'?
>>> text = 'a,b,c' >>> text = text.split(',') >>> text [ 'a', 'b', 'c' ]
Alternatively, you can use
eval() if you trust the string to be safe:
>>> text = 'a,b,c' >>> text = eval('[' + text + ']')
How to convert comma-delimited string to list in Python?, I wanted them in a python list, that is for each individual item to be treated as a string. Thus each item it needed to be enclosed by quotes. string_name.split (separator) Here, the separator is a comma (,). We store the list returned by split () method in a variable string_list. string_list=comma_string.split(",") string_list=comma_string.split (",") string_list=comma_string.split (",") Since we got what we needed, so we print the value of string_list.
Just to add on to the existing answers: hopefully, you'll encounter something more like this in the future:
>>> word = 'abc' >>> L = list(word) >>> L ['a', 'b', 'c'] >>> ''.join(L) 'abc'
But what you're dealing with right now, go with @Cameron's answer.
>>> word = 'a,b,c' >>> L = word.split(',') >>> L ['a', 'b', 'c'] >>> ','.join(L) 'a,b,c'
Convert string of comma separated values to python list – AndyPi, Converting a list into a comma-separated string results in a string where each element of the list is separated by a comma. For example, converting ["a", "b", "c"] � How To Convert Python String to List If we want to convert string to list operation, then we need to use the Python string split () method. Python split () method is used to split the strings and store them in the list. The split () method returns a list of the words in the string, using the “delimiter” as the delimiter string.
The following Python code will turn your string into a list of strings:
import ast teststr = "['aaa','bbb','ccc']" testarray = ast.literal_eval(teststr)
How to make a list into a comma-separated string 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. Given a string of comma separated numbers, we want to convert that string to a Python list of integers. Solution. The idea here is to split the string into tokens then convert each token to an integer. We can do that in a couple of ways. Let us see how… Use a list comprehension and the split function using the comma as a delimiter
I don't think you need to
In python you seldom need to convert a string to a list, because strings and lists are very similar
Changing the type
If you really have a string which should be a character array, do this:
In : x = "foobar" In : list(x) Out: ['f', 'o', 'o', 'b', 'a', 'r']
Not changing the type
Note that Strings are very much like lists in python
Strings have accessors, like lists
In : x Out: 'f'
Strings are iterable, like lists
In : for i in range(len(x)): ...: print x[i] ...: f o o b a r
Strings are lists. Almost.
How to convert a comma-separated string to a list in Python, Given a list of strings, write a Python program to convert the given list of strings is a string method and returns a string in which the elements of sequence have Python | Convert key-value pair comma separated string into� In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method. The split method is used to split the strings and store them in the list. The built-in method returns
In case you want to split by spaces, you can just use
a = 'mary had a little lamb' z = a.split() print z
['mary', 'had', 'a', 'little', 'lamb']
Python, Get a comma-separated string from a list of numbers. If you simply wish to convert a comma-separated string, then try the below shortcut: numList = [20, 213, 4587, 7869] print(str(numList).strip('')) The output is as follows: 20, 213, 4587, 7869. Alternatively, we can use map() function to convert the items in the list to a string.
join () function accepts an iterable sequence like list or tuple etc as an argument and then joins all items in this iterable sequence to create a string. In the end it returns the concatenated string. Let’s use this join () function to convert list to string in python.
To convert it into into a comma separated keys string key_string and a comma separated values string val_string I do the following:-key_list= val_list= for key,value in dict.iteritems(): key_list.append(key) val_list.append(value) key_string = ','.join(key_list) val_string = ','.join(val_list) The result is
I have a Python list that I'd like to turn into a comma-delimited string. This was my first thought: ",".join(lst2) but it bombs when the list contains anything other than strings.
- hi I did this like that, list(stringObject.split(','))
- @Clinteney: No problem ;-) You may also want to look into
pickleif you're persisting objects to strings.
- This is not an array, it is a list, Arrays represent basic values and behave very much like lists, except the type of objects stored in them is constrained.
- Friendly reminder: Don't use eval, even if you trust the string to be safe. It's not worth the potential security hazard for anything more than personal scripts and fiddling in an interactive shell.
- eval('[' + text + ']') will treat a,b,c as variables, not string.
- Downvoted because the two proposed solutions do completely different things, and this is not explained in any way in the answer text. While the question itself is not really clear, it should be made clear in the answer that the two solutions aren't equivalent alternatives, but differing interpretations of the question.
- Although I agree with you that @Cameron's answer was the "right now" answer I think your thoroughness makes this the more correct answer. We are rarely given comma separated strings out in the real world and it's good to know how to convert to a list regardless of format.
- They can also use
- This outputs what is between the
(), i.e. nothing at all.
- Out of curiosity, after you did myarray = array('c', text), where did the "c" go? since if you type array, you got array("c","abc"), but a==a? Thank you
- @Clinteney: The 'c' means "create an array of characters". See the docs for more info
- I've upvoted, but too early. I obtain array('c', '1 1 91 0.001 228') when calling a = array('c',e) where e = "1 1 91 0.001 228"
- @ValentinHeinitz Yes, that is what you must get as shown in my example. What do you expect ?