Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
String question
-
If I had a set of .csv values in a string:
00.1,00.2,00.3,00.4
What is a clever way to efficiently get them in an array?
-
print('00.1,00.2,00.3,00.4'.split(','))
['00.1', '00.2', '00.3', '00.4']
-
The simplest way would be:
csv_string = '00.1,00.2,00.3,00.4' csv_list = csv_string.split(',') # => ['00.1', '00.2', '00.3', '00.4']
or, if you need the result as a list of numbers:
csv_list = [float(x) for x in csv_string.split(',')] # => [0.1, 0.2, 0.3, 0.4]
This is a somewhat naive approach though, there are various "dialects" of csv, and if you're dealing with real data that you get from elsewhere, you'll probably want to look at the
csv
module (part of the standard library). -
Wow, brilliant. Now you guys are just showing off.....Thank you!
-
Sometimes I think their just making things up 😂😂