【问题标题】:Uploading two different Csv file in django with different Encoding在 django 中使用不同的编码上传两个不同的 Csv 文件
【发布时间】:2020-08-30 14:49:06
【问题描述】:

在我的 Django 管理员中,我有一个用于上传 csv 文件的按钮。我有两个文件,一个是 UTF-8 编码,一个是 ASCI/cp1252 编码。所以在我的代码中,如果我写

 data = pd.read_csv(value.file, encoding = "ASCI", engine='python')

上传了一个 csv 文件,但上传后的另一个文件在文本之间具有特殊字符。我不想上传特殊字符。 如果我写

 data = pd.read_csv(value.file, encoding = "UTF-8", engine='python')

显示特殊字符的一个不会出错,而另一个不会上传。 谁能告诉我如何解决这个问题? 下面是我的 Forms.py

class CsvUpload(forms.Form):
    csv_file = forms.FileField()

    def clean_csv_file(self):
        # Probably worth doing this check first anyway
        value = self.cleaned_data['csv_file']
        if not value.name.endswith('.csv'):
            raise forms.ValidationError('Invalid file type')

      
        try:
            data = pd.read_csv(value.file, encoding = "UTF-8", engine='python')
            data.columns= data.columns.str.strip().str.lower()
            data=data.rename(columns = {'test case id':'Test Case ID'})

        except Exception as e:
            print('Error while parsing CSV file=> %s', e)
            raise forms.ValidationError('Failed to parse the CSV file')
        if 'summary' not in data or 'Test Case ID' not in data:
            raise forms.ValidationError(
                'CSV file must have "summary" column and "Issue Key" column')
        return data

CSV 1.

  Test Case ID,Summary

TCMT-10,Verify that Process CSV sub module is displayed under “Process CSV” module on Dashboard of Client’s user.
TCMT-11,Verify that only View to “Duplicate test  cases” under “Test_Suite_Optimizer” module on Dashboard of Client’s user.
TCMT-12,Verify that Process CSV sub module is displayed under “Process CSV” module on Dashboard of Client’s user.
TCMT-13,Verify that toggle view is displayed on “Duplicate test cases” under “Test_Suite_Optimizer” module on Dashboard of Client’s user.
TCMT-14,Toggle view-? “Duplicate test cases” under “Test_Suite_Optimizer” module on Dashboard of Client’s user

CSV-2。

Test Case ID,summary
TC-16610,“verify that user is able to update 'active' attribute  'false ' on adding “new category records” using 'v3/definition/categories' PUT API on specifying the 'active' attribute 'true'”
TC-16609,“verify that user is able to update 'active' attribute  'true ' on adding “new category records” using 'v3/definition/categories' PUT API on specifying the 'active' attribute 'false'”

另外,在 csv-2 中,我在开放式办公室中添加了逗号。我想上传这个文件

【问题讨论】:

    标签: python django csv django-forms


    【解决方案1】:

    如果您尝试使用任何类型的编码读取文件,您可以编写动态代码来执行此操作。以下代码将首先打开一个文件并获取其编码。然后,它使用该编码创建 DataFrame

    # Get file encoding
    fileEncoding = None
    with open(value.file, "r") as f:
        fileEncoding = f.encoding
    data = pd.read_csv(value.file, encoding = fileEncoding, engine='python')
    

    【讨论】:

    • 你能分享两个文件或几行,以便我也可以测试它吗?
    • 共享。另外,在 csv-2 中,我还在开放式办公室中添加了逗号。我想上传这个文件
    • f.encoding 是用于打开文件的任何编码;如果open 中没有指定编码,它将是默认编码。它不会告诉你字节的编码。
    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2012-11-22
    • 2020-09-09
    相关资源
    最近更新 更多