如何删除^M [英] How to remove ^M

查看:39
本文介绍了如何删除^M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Python 脚本的文本文件(行尾)中删除 ^M 字符?

How can I remove the ^M character from a text file (at the end of line) in a Python script?

我做了以下操作,每个换行符都有^M.

I did the following, and there are ^M at every line-break.

file = open(filename, "w")
file.write(something)

推荐答案

如果你在写文件,你应该指定open(filename, "wb").这样,您将以二进制模式编写,Python 不会尝试为您所在的系统确定正确的换行符.

If you're writing the file, you should specify open(filename, "wb"). That way, you'll be writing in binary mode, and Python won't attempt to determine the correct newlines for the system you're on.

这篇关于如何删除^M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆