简体   繁体   English

<class ‘decimal.conversionsyntax’>使用 Python 从 CSV 文件导入数据时出错(尝试使用 CSV 和 Pandas)</class>

[英]<class ‘decimal.ConversionSyntax’> errors when importing data from a CSV file using Python (tried using CSV and Pandas)

I have a CSV file from a small business that represents their records for about 70,000 orders spread out over about 30 years.我有一个来自一家小型企业的 CSV 文件,该文件代表了他们在大约 30 年内分布的大约 70,000 个订单的记录。 My goal is to store this data in a Django project with a DecimalField in the model setup to receive the import.我的目标是将这些数据存储在 Django 项目中,并在 model 设置中使用 DecimalField 来接收导入。

Before trying to store the data in Django I'm first trying to import this data into Python.在尝试将数据存储在 Django 之前,我首先尝试将此数据导入 Python。 While most of the records import fine there are about 1 in 1000 that throw off a <class 'decimal.ConversionSyntax'> error during the import.虽然大多数记录导入正常,但在导入期间大约有 1000 条记录抛出 <class 'decimal.ConversionSyntax'> 错误。 This is causing me to get incorrect data into Python.这导致我将不正确的数据输入 Python。 I need to either fix how it reads the data or fix the problem in the data itself.我需要修复它读取数据的方式或修复数据本身的问题。

  1. I have tried both the python csv library and pandas to open the file.我已经尝试过 python csv 库和 pandas 来打开文件。
  2. I've looked at the csv file in a text editor (SublimeText) and can't find any anomalies with the way the data is stored.我在文本编辑器 (SublimeText) 中查看了 csv 文件,并没有发现数据存储方式有任何异常。
  3. I have tried to convert the data to UTF-8 using Microsoft Excel我尝试使用 Microsoft Excel 将数据转换为 UTF-8
  4. I have attempted to copy the data out of Excel into SublimeText, then back from SublimeText into a new row in Excel (virgin row with no data in it beforehand).我试图将数据从 Excel 复制到 SublimeText 中,然后从 SublimeText 复制到 Excel 中的新行(预先没有数据的原始行)。

None of this has resolved the issue.这些都没有解决问题。

How can I figure out what is wrong with this data and resolve this error?我怎样才能找出这些数据有什么问题并解决这个错误?

You can use chardet to detect the encoding.您可以使用chardet来检测编码。

import chardet
with open('csv_of_text.csv','rb') as fraw: # "rb" = bytes mode
    file_content = fraw.read()
chardet.detect(file_content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM