Python 在django中以不同编码上载两个不同的Csv文件
在我的Django管理员中,我有一个用于上传csv文件的按钮。我有两个文件,一个是UTF-8编码,另一个是ASCI/cp1252编码。所以在我的代码中,如果我写Python 在django中以不同编码上载两个不同的Csv文件,python,django,csv,django-forms,Python,Django,Csv,Django Forms,在我的Django管理员中,我有一个用于上传csv文件的按钮。我有两个文件,一个是UTF-8编码,另一个是ASCI/cp1252编码。所以在我的代码中,如果我写 data = pd.read_csv(value.file, encoding = "ASCI", engine='python') data = pd.read_csv(value.file, encoding = "UTF-8", engine='python') 一个csv文件被上传
data = pd.read_csv(value.file, encoding = "ASCI", engine='python')
data = pd.read_csv(value.file, encoding = "UTF-8", engine='python')
一个csv文件被上传,但另一个在上传后,文本之间有特殊的字符。我不想上传特殊字符。
如果我写
data = pd.read_csv(value.file, encoding = "ASCI", engine='python')
data = pd.read_csv(value.file, encoding = "UTF-8", engine='python')
显示特殊字符的一个不会给出错误,而另一个不会被上传。
谁能告诉我怎么解决这个问题吗?
下面是我的表单.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中,我在OpenOffice中添加了倒逗号。我希望上传此文件如果您试图读取任何类型编码的文件,您可以编写动态代码来执行此操作。下面的代码将首先打开一个文件并获取其编码。然后,它使用该编码创建数据帧:
#获取文件编码
fileEncoding=None
打开(value.file,“r”)作为f:
fileEncoding=f.encoding
data=pd.read\u csv(value.file,encoding=fileEncoding,engine='python')
您可以同时共享两个文件或几行,以便我也可以测试它吗?共享。另外,在csv-2中,我在open office中添加了倒逗号。我想上传这个文件f。编码是用来打开文件的任何编码;如果在open
中未指定编码,则它将是默认编码。它不会告诉您字节的编码。