CSV阅读器未获取列值

时间:2016-12-16 21:31:14

标签: python csv

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

我的代码未按预期访问第二列。而是返回第0列的第二个字符。不知道怎么回事。

3 个答案:

答案 0 :(得分:1)

Salah是对的,虽然我建议不要直接解析csv文件:

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

答案 1 :(得分:0)

您需要使用csv module直观地解析csv文件,否则您只需使用row.split(',')来获取值

答案 2 :(得分:0)

忘了这样做:

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