2017-04-26 CSV Files

posted Apr 26, 2017, 6:47 AM by Samuel Konstantinovich
given the string:
data = """1,2,3,4,5
6,7,8,9,10
12,0,-20,5
1,2,3,4,99"""
#remember three quotes will include newlines as \n, using one set of quotes will not. 

1. How would you convert that into a list of strings:  (newlines are just for ease of reading)
['1,2,3,4,5',
'6,7,8,9,10',
'12,0,-20,5',
'1,2,3,4,99']

2. How would you convert that into a list of lists of NUMBERS:(newlines are just for ease of reading)
[[1,2,3,4,5],
[6,7,8,9,10],
[12,0,-20,5],
[1,2,3,4,99]]

3. What if the 1st line of the data was a list of words:
data = """ID,age,height,width,score
1,2,3,4,5
6,7,8,9,10
12,0,-20,5
1,2,3,4,99"""

How can you make a special case for the first line such that you get the list:
[['ID','age','height','width','score'],
[1,2,3,4,5],
[6,7,8,9,10],
[12,0,-20,5],
[1,2,3,4,99]]

CSV Files are: Comma Separated Values
Some examples:

id,first_name,last_name,email,ip_address
1,Pablo,Gowlett,pgowlett0@google.com.au,102.186.140.76
2,Belvia,Kilbee,bkilbee1@cornell.edu,194.163.255.56
3,Hale,Brandli,hbrandli2@earthlink.net,249.248.161.249
4,Deonne,Horbath,dhorbath3@arizona.edu,68.48.203.96
5,Miles,Seery,mseery4@webmd.com,123.8.74.130
6,Cordi,Fitter,cfitter5@shinystat.com,18.174.34.11
7,Eran,Parkeson,eparkeson6@smh.com.au,128.206.255.200
8,Victor,Orr,vorr7@bbc.co.uk,16.103.244.100

CreditCardNumber,balance,DueDate
5610484586073360,$18564.57,5/25/2017
3553760169678731,$2641.36,1/16/2018
3538164882766651,$7930.95,7/3/2017
50180576168386114,$16275.92,3/12/2018
5108758453108683,$5801.57,1/16/2018
4917542453911013,$17801.02,4/9/2018
6304113713676600,$6276.45,9/20/2017
5002352103560175,$14832.72,9/4/2017

Real data can be found here:
Comments