python - How to write a list of values to text file with strings and numbers -
i have 3 lists :
alist = ['name','name2'] blist = ['desg1','desg2'] inlist = [1,2] i writing text file using follo code snippet:
fo = open(filepath, "w") in zip(alist,blist,inlist): lines=fo.writelines(','.join(i) + '\n') but getting follo error:
typeerror: sequence item 2: expected string, int found how can write values text file new line character.
join expects sequence of str items first argument, inlist contains int values. convert them str:
lines=fo.writelines(','.join(map(str, i)) + '\n') i'd suggest use with block while working files. write lines in single statement:
with open(filepath, "w") fo: fo.writelines(','.join(map(str, x)) + '\n' x in zip(alist,blist,inlist))
Comments
Post a Comment