简体   繁体   English

如何在Django中逐行读取csv文件?

[英]how to read csv file row by row in django?

I would like to make the first row in csv file as the field name in Django. 我想将csv文件中的第一行作为Django中的字段名称。 Is it possible? 可能吗? I would like to read the csv file row by row. 我想逐行阅读csv文件。

It's my code. 这是我的代码。

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=',')

but now I the reader can only store Alphabet cannot store words. 但是现在我的读者只能存储字母,不能存储单词。 my CSV file is like this: 我的CSV文件是这样的:

'registration_number', 'name', 'kana', 'month', 'day', 'client_code', 'row', 'business_days', 'working_days', 'count', 'early_late_time', 'normal_time', 'over_time', 'holiday_time', 'night_time', 'working_time', 'basic_salary', 'regular_attendance', 'reqular_attendace_allowance', 'over_time_ratio', 'holiday_time_ratio', 'night_time_ratio', 'fare_day', 'fare', 'employment_insurance', 'personally', 'spouse', 'dependent', 'special_allowance', 'adjustment', '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 'registration_number','name','kana','month','day','client_code','row','business_days','working_days','count','early_late_time','normal_time','over_time ','holiday_time','night_time','working_time','basic_salary','regular_attendance','reqular_attendace_allowance','over_time_ratio','holiday_time_ratio','night_time_ratio','fare_day','fare','employ' “个人”,“配偶”,“依赖”,“特殊津贴”,“调整”,“收入税”,“高级付款”,“暂记付款”,“医疗保险”,“就业养老金”,“就业保险金额”,“无付款”“ 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

I would like to use the first row as my field name, since every time the uploaded files got different format. 我想使用第一行作为字段名称,因为每次上传的文件都使用不同的格式。

thanks very much! 非常感谢!

Use csv.DictReader instead of csv.reader . 使用csv.DictReader而不是csv.reader With the fieldnames parameter omitted, the first row of the file will be used to determine field names. 省略fieldnames参数后,文件的第一行将用于确定字段名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM