3

I am trying to simply import a .csv into Python. I've read numerous documents but for the life of me I can't figure out how to do the following.

The CSV format is as follows

NYC,22,55
BOSTON,39,22

I'm trying to generate the following : {NYC = [22,55], BOSTON = [39,22]} so that I can call i[0] and i[1] in a loop for each variable.

I've tried

import csv
input_file = csv.DictReader(open("C:\Python\Sandbox\longlat.csv"))

for row in input_file:
print(row)

Which prints my variables, but I dont know hot to nest two numeric values within the city name and generate the list that im hoping to get.

Thanks for your help, sorry for my rookie question -

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Frank Drin
  • 1,613
  • 2
  • 13
  • 18

5 Answers5

5

Use dictionary comprehension:

import csv

with open(r'C:\Python\Sandbox\longlat.csv', mode='r') as csvfile:
    csvread = csv.reader(csvfile)
    result = {k: [int(c) for c in cs] for k, *cs in csvread}

This works in , and produces on my machine:

>>> result
{'NYC': [22, 55], 'BOSTON': [39, 22]}

It also works for an arbitrary number of columns.

In case you use , you can use indexing and slicing over sequence unpacking:

import csv

with open(r'C:\Python\Sandbox\longlat.csv', mode='r') as csvfile:
    csvread = csv.reader(csvfile)
    result = {row[0]: [int(c) for c in row[1:]] for row in csvread}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
5

If you are not familiar with python comprehensions, you can use the following code that uses a for loop:

import csv

with open('C:\Python\Sandbox\longlat.csv', 'r') as f:
    reader = csv.reader(f)
    result = {}
    for row in reader:
        result[row[0]] = row[1:]

The previous code works if you want the numbers to be string, if you want them to be numbers use:

import csv

with open('C:\Python\Sandbox\longlat.csv', 'r') as f:
    reader = csv.reader(f)
    result = {}
    for row in reader:
        result[row[0]] = [int(e) for i in row[1:]] # float instead of int is also valid
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
2

Each row will have 3 values. You want the first as the key and the rest as the value.

>>> row
['NYC','22','55']

>>> {row[0]: row[1:]}
{'NYC': ['22', '55']}

You can create the whole dict:

lookup = {row[0]: row[1:] for row in input_file}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
1

You can also use pandas like so:

import pandas as pd

df = pd.read_csv(r'C:\Python\Sandbox\longlat.csv')

result = {}
for index, row in df.iterrows():
    result[row[0]] = row[1:]
wnamen
  • 550
  • 3
  • 12
  • You are correct - I threw this in just because he is a rookie and may not know about what all his options are. – wnamen Oct 09 '17 at 18:01
  • also - I believe iterrows provides (index, row) tuples rather than just the rows, so the index is necessary – wnamen Oct 09 '17 at 18:06
-3

Heres a hint. Try familiarizing yourself with the str.split(x) function

 strVar = "NYC,22,55"
 listVar = strVar.split(',') # ["NYC", "22", "55"]
 cityVar = listVar[0]        # "NYC"
 restVar = listVar[1:];      # ["22", "55"]

 # If you want to convert `restVar` into integers
 restVar = map(int, restVar)
  • 2
    Noo... do not perform string operations yourself on a CSV file. CSV has some complex rules with respect to quotes, etc. – Willem Van Onsem Oct 09 '17 at 17:51
  • 1
    I don't think the answer deserves dv's. But usually it is better to parse standards using tools. Since these standards tend to be more complex than one thinks, and furthermore it is likely that libraries process content faster, more generic, less errors, etc. – Willem Van Onsem Oct 09 '17 at 19:13