gpt4 book ai didi

python - 接收 CSV 文件的脚本仅显示最后一行

转载 作者:行者123 更新时间:2023-12-01 06:38:13 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的脚本,将 CSV 文件转换为 JSON。我遇到的问题是它在 csv Order 列之前返回额外的字符。我是 Python 新手,如果我错过了以下信息,我很抱歉。我的资源和脚本是:

CSV

Order,Business_Unit,Sold_To,Ship_To,Customer_PO,Quantity_Ordered,UoM,Item_Number,Extended_Price,P4210_Version
1,M30,4242,4242,Line1,5,EA,210,,ZJDE0001
2,M30,4242,4242,Line2,6,EA,TPL0001,10,ZJDE0001

Python 脚本

import csv, json

csvFilePath = "DemoExcel.csv"
jsonFilePath = "DemoJson.json"

#Read the CSV and add the data to a dictionary...

data = {}
with open(csvFilePath) as csvFile:
csvReader = csv.DictReader(csvFile)
for csvRow in csvReader:
BusinessUnit = csvRow["Order"]
data[BusinessUnit] = csvRow

#Write data to a JSON file...

with open(jsonFilePath, "w") as jsonFile:
jsonFile.write(json.dumps(data, indent=4))

一开始它无法成功运行。所以我做了一个打印(数据)并看到 CSV 被读取为:

{'1': OrderedDict([('Order', '1'), ('Business_Unit', 'M30'), ('Sold_To', '4242'), ('Ship_To', '4242'), ('Customer_PO', 'Line1'), ('Quantity_Ordered', '5'), ('UoM', 'EA'), ('Item_Number', '210'), ('Extended_Price', ''), ('P4210_Version', 'ZJDE0001')]), '2': OrderedDict([('Order', '2'), ('Business_Unit', 'M30'), ('Sold_To', '4242'), ('Ship_To', '4242'), ('Customer_PO', 'Line2'), ('Quantity_Ordered', '6'), ('UoM', 'EA'), ('Item_Number', 'TPL0001'), ('Extended_Price', '10'), ('P4210_Version', 'ZJDE0001')]), '3': OrderedDict([('Order', '3'), ('Business_Unit', '30'), ('Sold_To', '4242'), ('Ship_To', '4242'), ('Customer_PO', 'Bell Media'), ('Quantity_Ordered', '209'), ('UoM', 'EA'), ('Item_Number', '210'), ('Extended_Price', '23456'), ('P4210_Version', 'ZJDE0002')]), '4': OrderedDict([('Order', '4'), ('Business_Unit', '30'), ('Sold_To', '4242'), ('Ship_To', '4242'), ('Customer_PO', 'AT&T'), ('Quantity_Ordered', '3'), ('UoM', 'M'), ('Item_Number', '210'), ('Extended_Price', ''), ('P4210_Version', 'ZJDE0002')])}

我注意到订单显示为“订单”而不是“订单”。所以我改变了我的Python以包含“Order”

import csv, json

csvFilePath = "DemoExcel.csv"
jsonFilePath = "DemoJson.json"

#Read the CSV and add the data to a dictionary...

data = {}
with open(csvFilePath) as csvFile:
csvReader = csv.DictReader(csvFile)
for csvRow in csvReader:
Order = csvRow["Order"]
data[Order] = csvRow

print(data)

#Write data to a JSON file...
#"w" argument is to indicate it's being written to...
with open(jsonFilePath, "w") as jsonFile:
jsonFile.write(json.dumps(data, indent=4))

现在已成功创建 JSON 文件,但订单返回为

{
"1": {
"\u00ef\u00bb\u00bfOrder": "1",
"Business_Unit": "M30",
"Sold_To": "4242",
"Ship_To": "4242",
"Customer_PO": "Line1",
"Quantity_Ordered": "5",
"UoM": "EA",
"Item_Number": "210",
"Extended_Price": "",
"P4210_Version": "ZJDE0001"
},
"2": {
"\u00ef\u00bb\u00bfOrder": "2",
"Business_Unit": "M30",
"Sold_To": "4242",
"Ship_To": "4242",
"Customer_PO": "Line2",
"Quantity_Ordered": "6",
"UoM": "EA",
"Item_Number": "TPL0001",
"Extended_Price": "10",
"P4210_Version": "ZJDE0001"
},
"3": {
"\u00ef\u00bb\u00bfOrder": "3",
"Business_Unit": "30",
"Sold_To": "4242",
"Ship_To": "4242",
"Customer_PO": "Bell Media",
"Quantity_Ordered": "209",
"UoM": "EA",
"Item_Number": "210",
"Extended_Price": "23456",
"P4210_Version": "ZJDE0002"
},
"4": {
"\u00ef\u00bb\u00bfOrder": "4",
"Business_Unit": "30",
"Sold_To": "4242",
"Ship_To": "4242",
"Customer_PO": "AT&T",
"Quantity_Ordered": "3",
"UoM": "M",
"Item_Number": "210",
"Extended_Price": "",
"P4210_Version": "ZJDE0002"
}
}

有没有办法让它只返回 Order 而不是 \u00ef\u00bb\u00bfOrder ?我使用的是通过在 Excel 中另存为 .csv 制作的 CSV。当我在 Sublime Text Editor 中打开 CSV 时,我没有看到任何额外的字符。

我不知道如何让它只返回 name : key 对的 Order。

最佳答案

根据 CSV 变量名称,该数据来自 Excel,这意味着该文件是使用 utf-8-sig 编码保存的,并预先添加了 BOM ( Byte order mark )。当您在 Python 中打开文件而不指定编码时,它将采用编码(Python 3 中的 utf-8,Python 2 中的 ascii)并将 BOM 解释为任何编码文件中的其他字节。

要解决这个问题,您只需告诉 Python 正确的编码

Python 3:

...

with open(csvFilePath, encoding='utf-8-sig') as csvFile:

...

Python 2:

import codecs

...

with codecs.open(csvFilePath, encoding='utf-8-sig') as csvFile:

...

关于python - 接收 CSV 文件的脚本仅显示最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59568925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com