【问题标题】:Script to take in a CSV file only displays last row [duplicate]接收 CSV 文件的脚本仅显示最后一行 [重复]
【发布时间】:2020-04-21 10:45:25
【问题描述】:

我正在尝试做一个将 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))

起初它无法成功运行它。所以我做了一个 print(data) 并看到 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')])}

我注意到 Order 显示为 Order 而不是 Order。所以我改变了我的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 文件,但 Order 被返回为

{
    "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" 更改为"Order"。仅供参考,json.dump(jsonFile, indent=4) 应该足够了,除非它因为编码而失败
  • 您的输入 .csv 文件是什么字符集?如果它不是简单的 ASCII,你将不得不在输入时清理它......
  • 这能回答你的问题吗? Python read csv - BOM embedded into the first key
  • 供将来参考:Sublime Text 应在右下角显示“UTF-8 with BOM”而不是“UTF-8”。 BOM 不显示为字符,但如果您在十六进制编辑器中打开文件,您可以看到它们。

标签: python json csv


【解决方案1】:

根据 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:

...

【讨论】:

  • 非常感谢!这对我有用,我非常感谢清晰的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多