在我的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 dataCSV 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 userCSV-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中,我在开放办公室中添加了反逗号。我想把这个文件上传
发布于 2020-08-30 22:56:24
如果您尝试以任何类型的编码读取文件,则可以编写动态代码来执行此操作。下面的代码将首先打开一个文件并获取其编码。然后,它使用该编码创建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')https://stackoverflow.com/questions/63658668
复制相似问题