Forum Archive

String question

donnieh

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?

ccc

print('00.1,00.2,00.3,00.4'.split(','))

['00.1', '00.2', '00.3', '00.4']

omz

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).

donnieh

Wow, brilliant. Now you guys are just showing off.....Thank you!

reefboy1

Sometimes I think their just making things up 😂😂