Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract numbers from between commas on each row is csv file

I need to figure out how to separate 3 values from rows in a CSV file into new lists to later plot onto a graph. I already have separated each row into a list, but I need new lists that categorize each comma-separated value.

I already separated each row into a list as seen below:

import csv

with open('Lottery_Powerball_Winning_Numbers__Beginning_2010.csv', 'r') as readObj:
    heading = next(readObj)
    csvReader = csv.reader(readObj)
    listOfCSV = list(csvReader)
    print(listOfCSV)

The output is paraphrased here (Cannot put full output due to character limits.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['9/26/20', '11 21 27 36 62 24', '3'], ['9/30/20', '14 18 36 49 67 18', '2'], ['10/3/20', '18 31 36 43 47 20', '2'], ['10/7/20', '06 24 30 53 56 19', '2'], ['10/10/20', '05 18 23 40 50 18', '3']

>Solution :

You can use a for loop to iterate through the list of rows and use the .split() method to separate each comma-separated value into a new list. Here’s an example:

date_list = []
numbers_list = []
multiplier_list = []

for row in listOfCSV:
    date_list.append(row[0])
    numbers_list.append(row[1].split())
    multiplier_list.append(row[2])

print(date_list)
print(numbers_list)
print(multiplier_list)

This will create three new lists: date_list will contain the first value of each row (the date), numbers_list will contain the second value of each row (the numbers) separated into a list, and multiplier_list will contain the third value of each row (the multiplier). Now you can use this data to plot your graph.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading