python - Write to csv and I get error : _csv.Error: sequence expected -


please help. error when want write dict alllistinstance csv file. code:

alllistinstance= {frozenset(['offense involving children']): [(95,), (96,), (35,), (80,), (100,)], frozenset(['battery', 'theft']): [(173, 209), (173, 224)]}  open('test1.csv', 'wb') csv_file:    key in alllistinstance.keys():     csv_writer = csv.writer(csv_file)     csv_writer.writerow(len(alllistinstance[key]))     y in range(len(key)):         csv_writer.writerow([x[y] x in key])         csv_writer.writerow(x[y] x in alllistinstance[key]) 

output expected:

5   # len(alllistinstance["offense involving children"]) count of member offense involving children 95 96 35 80 100 2 # len(alllistinstance['battery','thief']) count of member battery        thief 173            209 173            224 

error:

    csv_writer.writerow(len(alllistinstance[key])) _csv.error: sequence expected 

solusion expected ouput:

with open('test8.csv', 'wb') csv_file:   key in alllistinstance.keys():     csv_writer = csv.writer(csv_file)     csv_writer.writerow([len(key),len(alllistinstance[key])])     csv_writer.writerow(list(key))     x in alllistinstance[key]:         csv_writer.writerow(list(x)) 

you passing in single integer:

len(alllistinstance[key]) 

that's not sequence, csv_writer.writerow() expects. if want write 1 column length value, wrap in list:

csv_writer.writerow([len(alllistinstance[key])]) 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -