write a list to file but error : writelines() argument must be a sequence of strings
本问题已经有最佳答案,请猛点这里访问。
我有一个列表,我想将该列表写入文件txt
1 2 3 4 5 | lines=[3,5,6] result = open("result.txt","w") result.writelines(lines) result.close() |
但是当我跑步时,我收到以下错误:
writelines() argument must be a sequence of strings
错误是不言自明的:您必须传递一系列字符串,而不是数字到
1 2 3 4 | lines = [3, 5, 6] with open("result.txt","w") as f: f.writelines([str(line) +" " for line in lines]) |
好吧,你给它一个整数列表,它明确地告诉你它需要一系列字符串。
没有义务是不礼貌的:
1 | result.writelines(str(line) for line in lines) |