【发布时间】: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