python - ValueError: dict contains fields not in fieldnames: None -
i writing code filter values in csv file , write relevant rows 1 file, accepted not desired rows , error files third file.
this i've writen:
#variables doc = '/lab01/projects/lords_projects/mna_exons.csv' save = '/lab01/projects/lords_projects/mna_exac.csv' other = '/lab01/projects/lords_projects/mna_alt.csv' error = '/lab01/projects/lords_projects/mna_error.csv' seek = 'exac_all' comp = .05 #script import csv #open read = open(doc, 'rb') write = open(save, 'wb') dif = open(other, 'wb') mis = open(error, 'wb') #text conversion reader = csv.dictreader(read, delimiter=',') copy = csv.dictwriter(write, delimiter='\t', fieldnames=reader.fieldnames) alt = csv.dictwriter(dif, delimiter='\t', fieldnames=reader.fieldnames) er = csv.dictwriter(mis, delimiter='\t', fieldnames=reader.fieldnames) #headers head = {} n in reader.fieldnames: head[n] = n copy.writerow(head) alt.writerow(head) er.writerow(head) #sorting row in reader: try: if float(row[seek]) < comp , float(row[seek]) > -comp: copy.writerow(row) elif float(row[seek]) >= comp or float(row[seek]) <= -comp: alt.writerow(row) except: try: if row[seek] == 'n/a': copy.writerow(row) except: prob = csv.writer(write, delimiter='\t') er.writerow(row) prob.writerow("value not float\n") #close read.close() write.close() dif.close() mis.close() #error check if 'value not float\n' in open(save, 'r').read(): print('error') open(save, 'r').close()
when run it, gives me error message:
traceback (most recent call last): file "csv_cond_filter.py", line 45, in er.writerow(row) file "/usr/lib64/python2.7/csv.py", line 152, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) file "/usr/lib64/python2.7/csv.py", line 148, in _dict_to_list + ", ".join([repr(x) x in wrong_fields])) valueerror: dict contains fields not in fieldnames: none
to test see might wrong, made error line comment , ran script again. did not give me error message, nor did print error value specified write if not classify segment, suspect syntax error, i'm new python, , don't know enough csv module or dictwriter.
try display contents of row: replace
er.writerow(row)
with
try: er.writerow(row) except exception exc: exc.args += (row,) raise
Comments
Post a Comment