首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在django中用不同的编码上传两个不同的Csv文件

在django中用不同的编码上传两个不同的Csv文件
EN

Stack Overflow用户
提问于 2020-08-30 22:49:07
回答 1查看 47关注 0票数 0

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

代码语言:javascript
复制
 data = pd.read_csv(value.file, encoding = "ASCI", engine='python')

一个csv文件已上载,但另一个文件在上载后在文本之间有特殊字符。我不希望上传特殊字符。如果我写下

代码语言:javascript
复制
 data = pd.read_csv(value.file, encoding = "UTF-8", engine='python')

显示特殊字符的那个不会给出错误,而另一个不会被上传。有人能告诉我怎么解决这个问题吗?下面是我的Forms.py

代码语言:javascript
复制
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。

代码语言:javascript
复制
  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。

代码语言:javascript
复制
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中,我在开放办公室中添加了反逗号。我想把这个文件上传

EN

回答 1

Stack Overflow用户

发布于 2020-08-30 22:56:24

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

代码语言:javascript
复制
# 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')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63658668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档