2

this is my code

f= open("average-latitude-longitude-countries.csv", "r")
for line in f:
    line = line.strip("\n")

    elements = line.split(",")
    code = elements[0].strip('"')
    elements[1] = elements[1].strip('"')
    if len(elements) == 4:
        name = elements[1]
        latitude = float(elements[2])
        longitude = float(elements[3])
    else:
        elements[2] = elements[2].strip('"')
        name = elements[2] + elements[1]
        latitude = float(elements[3])
        longitude = float(elements[4])
    list[]
    list.append(code, name, latitude, longitude)

this csv file is as follows

AD  Andorra 42.5    1.5
AE  United Arab Emirates    24  54
AF  Afghanistan 33  65
AG  Antigua and Barbuda 17.05   -61.8
AI  Anguilla    18.25   -63.17

what i want to do is read the code, country name, latitude, longitude into

list[(code, name, lat, long)
     (code, name, lat, long)]

such format.

i've just started python and i've tried "for line in f" and read documents in appending items into list. however what i cant' seem to understand is how i add four items of different attribute into that list.

and i can't seem to understand when to use list and dictionary. dictionary is immutable? i can't seem to see the merit for this yet. and would dictionary would be better format for this?

  • What do you mean by `list[]`? Did you mean `list = []`? If so, define empty list outside the loop. Also don't use keywords as variable name – kuro May 17 '17 at 16:48
  • ah, yes. i mean list = [] should've ctrl + c , v'd. when i do that i get "can take one argument only". i wanted to get the four items into one list. thank i've changed the variable name. will always becareful with the names. – Sunwoong ENFYS Cho May 17 '17 at 16:50
  • You code do `list.append((code, name, latitude, longitude))`. It will insert four values as a tuple – kuro May 17 '17 at 16:53
  • in python you use `l.append((1, 2, 3, 4,))` to add a tuple or just `row = (1,2,3,4); l.append(row)` for greater readability – Serge May 17 '17 at 16:53
  • @kuro `list` is not a keyword. But nevertheless it shouldn't be used for custom variables. – a_guest May 17 '17 at 16:54
  • @kuro wow, i didn't know it would be that simple. real big thanks! – Sunwoong ENFYS Cho May 17 '17 at 16:59

3 Answers3

2

Don't reinvent the wheel, use a library instead, e.g. pandas:

import pandas as pd
df = pd.read_csv(your_file_here, sep='\t')

Returns a dataframe which you can access in a dict-like format.

Jan
  • 42,290
  • 8
  • 54
  • 79
2

You can probably use str.split()

f = open("average-latitude-longitude-countries.csv", "r")

l = []
for line in f:
    x = tuple(str(line).split())
    l.append(x)

print l
Adhish
  • 95
  • 2
  • 5
0

Just use the standard csv library, one day you might step on some csv perk or even, specialized library PTable if just need to prettyprint, numpy np.loadtxt for heavy data lifting etc.

https://docs.python.org/2/library/csv.html

see similar examples in the previous discussion

Python import csv to list

import csv

with open('file.csv', 'r') as f:
  reader = csv.reader(f)
  your_list = list(reader)  # Python 3
Community
  • 1
  • 1
Serge
  • 3,387
  • 3
  • 16
  • 34