Python-Can';无法从CSV文件读取字典值

Python-Can';无法从CSV文件读取字典值,python,csv,Python,Csv,我有两种不同的CSV文件。其中一个使用双引号,另一个不使用 A: "shipment_id","status","to_name","to_address_1" etc B: shipment_id,status,to_name,to_address_1 etc 无论提交哪种类型的CSV,如何读取CSV并打印装运id的值 当CSV不使用双引号时,我的代码似乎不起作用 with open(file_location) as f_obj: reader = csv.DictReader(

我有两种不同的CSV文件。其中一个使用双引号,另一个不使用

A: "shipment_id","status","to_name","to_address_1" etc

B: shipment_id,status,to_name,to_address_1 etc
无论提交哪种类型的CSV,如何读取CSV并打印
装运id
的值

当CSV不使用双引号时,我的代码似乎不起作用

with open(file_location) as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',')
    for line in reader:
         print(line['shipment_id'])
试试这个:

with open(file_location) as f_obj:
    f_obj = f_obj.read().replace('"','').splitlines()
    reader = csv.DictReader(f_obj, delimiter=',')
    for line in reader:
         print(line['shipment_id'])
.replace(“,”)
如果有双引号,它将工作,如果没有双引号,它将不起任何作用

让我知道它是否有效:)

基于我认为.csv文件应该是什么样子以及pandas read_csv的经验,我决定如下给出我的输入

test.csv文件的示例

test1.csv文件的示例

带有打印的test.csv指定字段名的代码(第['shipping\u id'行]):

import csv
with open('test.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader_ddQ = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader_ddQ:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line)
输出:

1233
9999
321
980
1980
OrderedDict([('shipment_id', '321'), ('status', 'ok'), ('to_name', 'P'), ('to_address_1', 'A')])
OrderedDict([('shipment_id', '980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])
OrderedDict([('shipment_id', '1980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])
带打印的test1.csv的指定字段名代码(第['shipping\u id'行]):

import csv
with open('test.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader_ddQ = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader_ddQ:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line)
输出:

1233
9999
321
980
1980
OrderedDict([('shipment_id', '321'), ('status', 'ok'), ('to_name', 'P'), ('to_address_1', 'A')])
OrderedDict([('shipment_id', '980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])
OrderedDict([('shipment_id', '1980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])
带有指定字段名的代码,用于带有打印(行)的test1.csv:

import csv
with open('test.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader_ddQ = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader_ddQ:
        print(line['shipment_id'])
with open('test1.csv') as f_obj:
    reader = csv.DictReader(f_obj, delimiter=',', fieldnames=['shipment_id','status','to_name','to_address_1'])
    for line in reader:
        print(line)
输出:

1233
9999
321
980
1980
OrderedDict([('shipment_id', '321'), ('status', 'ok'), ('to_name', 'P'), ('to_address_1', 'A')])
OrderedDict([('shipment_id', '980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])
OrderedDict([('shipment_id', '1980'), ('status', 'No'), ('to_name', 'A'), ('to_address_1', 'G')])

的源您应该能够使用
quotechar
作为参数:

reader = csv.DictReader(f_obj, delimiter=',', quotechar='"')
(或者可能是
“\”
——我不知道Python是如何处理这个问题的)

这应该适用于数据的两个版本


如果
DictReader
不支持
quotechar
参数,请尝试直接在
csv.reader
上使用它。

您列为数据的内容看起来不像DictReader。。。请提供一个更好的例子。@sb0709哈?OP解释说这是一个csv。为什么你认为它是一个
dict
?根据示例数据不太清楚:a:“发货id”、“状态”、“收件人姓名”、“收件人地址”等,因此产生了多个dict的csv印象…它在这里起作用。我想OP希望将“csv”中的数据转换成dict。。。因为这是DictReader进行一些测试的原因,代码可以工作,可能只是csv中的数据没有输出的问题,或者需要在读取时指定列名,或者格式化“csv”以包括每个列的名称。下面是我从正确格式的“csv”OrderedICT([(“发货id”,“1233”),(“状态”,“否”),(“收件人名称”,“N”),(“收件人地址”,“C”))中看到的输出