with open(r'E:\test.txt', 'a+') as f:
f.write(str(a))
f.write(str(a))
|
1
with open(r'E:\test.txt', 'a+') as f:
----f.write(str(a) + os.linesep) |
|
kevinyoungMay 3, 2015
2
"\n"?
|
|
3
看你是什么系统了,如果代码自动兼容的话,看1楼的
如果目标系统很明确,可以直接写代码里面 *nux “\n” Windows “\r\n“ |
|
4
我的是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 弄了一个晚上了
|
|
v2014May 3, 2015
6
|
|
ohyeah521May 4, 2015
7
os.linesep 是最好的选择
|
|
onlyiceMay 4, 2015
8
看情况决定使用 os.linesep 还是一律 \n
如果你生成的文件需要交给 *nix 环境去读取,建议使用 \n |
|
9
不知道哪里出错了,请大家指出,到最后没有输出全部,只能得到 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) |