我想将csv文件中的第一行作为Django中的字段名称。 可能吗? 我想逐行阅读csv文件。
这是我的代码。
if request.FILES:
form = SalaryForm(request.POST, request.FILES)
if form.is_valid():
# request.session['salary'] = request.POST["salary"]
# salary = Salary.object.get(id=request.session['salary'])
print(request.FILES['fileUpload'].name)
csvfile = request.FILES['fileUpload'].read()
encoding = chardet.detect(csvfile)['encoding']
print (encoding)
if encoding is None:
encoding = 'CP932'
if encoding !='utf-8':
csvfile = csvfile.decode(encoding, 'replace').encode('utf-8')
reader = csv.reader(csvfile, delimiter=',')
但是现在我读者只能存储不能存储字母的字母。
我的CSV文件是这样的:
' registration_number',' name',' kana',' month',' day',&# 39; client_code',' row',' business_days',' working_days',' count',' early_late_time&#39 ;,' normal_time',' over_time',' holiday_time',' night_time',' working_time',&#39 ; basic_salary',' regular_attendance',' reqular_attendace_allowance',' over_time_ratio',' holiday_time_ratio',' night_time_ratio' ,' fare_day',' fare',' employment_insurance','个人','配偶','依赖',' special_allowance','调整',' income_tax',' advanced_payment',' suspense_payment', ' health_insurance',' employment_pension',' employment_insurance_amount',' no_payment' " 223","山野薫","ヤマノカオリ",1,10," 1101",1,17,16, ,\1 ,,,,, \8,800,350,0,\1,\1,\0,8300,1,0,9,0,9200,2260 ,,, 9557,15928,818,0 " 018","波多野未树","ハタノミキ",1,15," 2301",1,21,20, ,,,,,, \8,800,200,0,\1,\1,\0,300,6000,0 ,,,,,, 3360 ,,,,,, 0
我想使用第一行作为我的字段名称,因为每次上传的文件都有不同的格式。
非常感谢!