|  |      1messense      2015-05-03 21:31:58 +08:00  3 with open(r'E:\test.txt', 'a+') as f: ----f.write(str(a) + os.linesep) | 
|  |      2kevinyoung      2015-05-03 21:52:12 +08:00 "\n"? | 
|  |      3goodryb      2015-05-03 21:57:18 +08:00  1 看你是什么系统了,如果代码自动兼容的话,看1楼的 如果目标系统很明确,可以直接写代码里面 *nux “\n” Windows “\r\n“ | 
|  |      4onlinezu OP 我的是windows,为什么我搜到 http://stackoverflow.com/questions/21636213/why-you-shouldnt-use-os-linesep-when-editing-on-text-mode ,如果写成 f.write(str(word) + \r\n),提示SyntaxError: unexpected character after line continuation character 弄了一个晚上了 | 
|  |      6v2014      2015-05-03 22:53:56 +08:00 | 
|  |      7ohyeah521      2015-05-04 08:18:48 +08:00 os.linesep  是最好的选择 | 
|  |      8onlyice      2015-05-04 09:35:19 +08:00 看情况决定使用 os.linesep 还是一律 \n 如果你生成的文件需要交给 *nix 环境去读取,建议使用 \n | 
|  |      9onlinezu OP 不知道哪里出错了,请大家指出,到最后没有输出全部,只能得到 a result1 = [1,"2", "a"] for item in result1: print(item) with open(r'E:\tools\fenci1.txt', 'a+') as file: file.write("\r\n") file.write(str(item) + "\r\n") file.write("".join(str(item)) + os.linesep) |