0
db_path = "./db_file.csv"
db_read = open(db_path, "r") 
for row in db_read:
    g_tag = (row)[1]

My code is not accessing the second column as expected. Instead it is returning the second character of the 0th column. Don't get whats going on.

2
  • can you provide the first few lines of your csv file as an example to work with? Commented Dec 16, 2016 at 21:33
  • Give your csv data, there might be somehting worng with data. Commented Dec 16, 2016 at 21:41

3 Answers 3

1

Salah is right, although I'd advise against parsing a csv file directly:

In [63]: with open('output.csv', 'r') as f:
    ...:     reader = csv.reader(f)
    ...:     for row in reader:
    ...:         print row
    ...:
['latitude', 'local_time', 'longitude', 'time']
['51.2997804', '20:01:14:334 11 08 2015 +0100 GMT+01:00', '1.070336', '1439319674334']
['51.2997889', '20:01:34:428 11 08 2015 +0100 GMT+01:00', '1.0703332', '1439319694428']
['51.2997794', '20:01:54:638 11 08 2015 +0100 GMT+01:00', '1.0703123', '1439319714638']
Sign up to request clarification or add additional context in comments.

Comments

0

you need to use the csv module to parse csv files intuitively, otherwise you can just use row.split(',') to get values

Comments

0

Forgot to do this:

        db_read = open(db_path, 'r')
        db_reader = csv.reader(db_read)

1 Comment

If you are looking to follow best practice, I would very much encourage you to use a context manager (with open('your.csv', 'r') as file: ) this will close the file object for you, its a good habit to get in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.