我正在尝试使用.DictReader从csv文件读取,并基于从标题(键)读取的值,我想使用.DictWriter写入新的csv文件。我收到一个错误,提示ValueError:无法将字符串转换为float。
据我了解,DictReader将获得一个字符串列表,而不只是字符串,因此它无法直接转换为csv.DictReader正在读取的内容。因此,我尝试遍历整个列表。它仍然给我一些错误。
第一个代码:
import csv
with open('report.csv', 'r') as openfile: #open report
csv_reader = csv.DictReader(openfile, delimiter='\t')
#writing to a new file start
#sets up the output file output.csv
with open('output.csv', 'w') as new_file:
#hardcoding the filename
fieldnames = csv_reader.fieldnames
fieldnames = ['header1', 'header2', 'header3']
#setting the parameters for the output file
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t', extrasaction='ignore')
csv_writer.writeheader()
for line in csv_reader: #checking every line we are reading
headerval= line['header2']
if float(header2val) >= 200: #check condition
csv_writer.writerow(line) #writes if true
然后我尝试遍历演员表(不确定是否正确)
import csv
with open('report.csv', 'r') as openfile: #open report
csv_reader = csv.DictReader(openfile, delimiter='\t')
#writing to a new file start
#sets up the output file output.csv
with open('output.csv', 'w') as new_file:
#hardcoding the filename
fieldnames = csv_reader.fieldnames
fieldnames = ['header1', 'header2', 'header2']
#setting the parameters for the output file
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t', extrasaction='ignore')
csv_writer.writeheader()
for line in csv_reader: #checking every line we are reading
for checkval in line['header2']: #iterate the casting
headerval= float(checkval)
if headerval >= 200: #check condition
csv_writer.writerow(line) #writes if true
第一个代码错误消息:“ TypeError float()参数必须是字符串或数字” 预期的输出结果是正确的。标头和打印到output.csv的值满足代码1中的条件。
第二个代码错误消息:“ ValueError:无法将字符串转换为浮点数” 在代码2中,仅打印标题。
编辑:report.csv
header1 header2 header3 header4 header5
1 30.35 true true false
2 20.35 false true false
3 50.35 true true false
4 10.35 true true false
5 20.35 true true false
6 70.35 false true false
7 85.26 false true false
8 83.39 true true false
9 172.11 true true false
10 184.99 false true false
11 146.11 true true false
12 230.28 false true false
13 124.42 false true false
14 416.15 true true false
15 257.27 false true false
16 263.39 true true false
17 295.0 true true false
18 175.35 true true false
19 275.62 true true false
20 189.08 true true false
21 163.05 true true false
22 166.66 false true false
23 186.9 false true false
24 181.42 false true false
25 181.18 false true false
26 184.12 false true false
27 177.27 false true false
28 238.61 true true false
29 163.88 true true false
30 204.12 false false false
31 215.22 true true false
32 166.41 true true false
33 143.49 true true false
34 181.31 true true false
35 431.25 false false false
36 245.3 false false false
37 245.89 false false false
38 251.72 true true false
39 161.89 false false false
40 210.83 true true false
41 188.25 false false false
42 186.48 true true false
43 205.49 false false false
44 184.07 true true false
45 144.83 true true false
46 167.21 true true false
47 181.11 false false false
48 183.73 true true true
49 175.57 true true false
答案 0 :(得分:0)
我认为report.csv
文件的标题格式可能不正确,这使使用DictReader
读取文件感到混乱—因此,这是一种变通方法,至少可以在第一部分获得代码您的问题起作用。它会硬编码读者应该使用的字段名称,而忽略标题行。
import csv
input_filename = 'report.csv'
output_filename = 'output.csv'
fieldnames = ['header1', 'header2', 'header3'] # Hardcode the fieldnames.
with open(input_filename, 'r', newline ='') as openfile:
csv_reader = csv.DictReader(openfile, fieldnames=fieldnames, delimiter='\t')
next(csv_reader) # Skip badly formatted header.
with open(output_filename, 'w', newline ='') as new_file:
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t',
extrasaction='ignore')
csv_writer.writeheader()
for line in csv_reader:
header2val= line['header2'] # Get second column.
if float(header2val) >= 200: # Check value.
csv_writer.writerow(line)