DGT*_*DGT 9 python string end-of-line
如何^M从Python脚本中的文本文件(行尾)中删除字符?
我做了以下工作,并且^M每次突破都有.
file = open(filename, "w")
file.write(something)
Run Code Online (Sandbox Code Playgroud)
Python可以以二进制模式或文本模式打开文件.文本是默认值,因此"w"模式表示以文本模式写入.在文本模式下,Python将调整您所在平台的行结尾.这意味着在Windows上,此代码:
f = open("foo.txt", "w")
f.write("Hello\n")
Run Code Online (Sandbox Code Playgroud)
将导致包含"Hello\r \n"的文本文件.
您可以在模式中使用"b"以二进制模式打开文件:
f = open("foo.txt", "wb")
f.write("Hello\n")
Run Code Online (Sandbox Code Playgroud)
导致包含"Hello \n"的文本文件.
| 归档时间: |
|
| 查看次数: |
21295 次 |
| 最近记录: |