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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -