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

Time:01-23

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

['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']

CodePudding user response:

You can make three new lists and read the list of rows you have obtained previously.

csv = [['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']]

date = []
number = []
end = []

for row in csv:
  date.append(row[0])
  number.append(row[1])
  end.append(row[2])

print(date)
print(number)
print(end)

I will produce

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

The code is https://onecompiler.com/python/3yvns443z

CodePudding user response:

If you build a dictionary then that will make it easy to access values by date. For example, let's assume that the CSV file contains these data:

Date, Numbers, Multiplier
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

...then...

import json # Only needed to prettify the output

data = dict()

with open('Lottery_Powerball_Winning_Numbers__Beginning_2010.csv') as csv:
    next(csv) # skip column header
    for line in csv:
        try: # use try/ except in case any line does not contain exactly 3 tokens
            date, numbers, multiplier = line.split(',')
            data[date] = {'numbers': list(map(int, numbers.split())), 'multiplier': int(multiplier)}
        except ValueError:
            pass

print(json.dumps(data, indent=2))

Output:

{
  "9/26/20": {
    "numbers": [
      11,
      21,
      27,
      36,
      62,
      24
    ],
    "multiplier": 3
  },
  "9/30/20": {
    "numbers": [
      14,
      18,
      36,
      49,
      67,
      18
    ],
    "multiplier": 2
  },
  "10/3/20": {
    "numbers": [
      18,
      31,
      36,
      43,
      47,
      20
    ],
    "multiplier": 2
  },
  "10/7/20": {
    "numbers": [
      6,
      24,
      30,
      53,
      56,
      19
    ],
    "multiplier": 2
  },
  "10/10/20": {
    "numbers": [
      5,
      18,
      23,
      40,
      50,
      18
    ],
    "multiplier": 3
  }
}
  • Related