【问题标题】:Python outputs a .txt file, which's format differs depending on the text editor I use to open itPython 输出一个 .txt 文件,其格式因我用来打开它的文本编辑器而异
【发布时间】:2015-06-23 09:30:23
【问题描述】:

所以我有一些 python 代码可以将一些数据输出到 .txt 文件,如下所示:

f3 = codecs.open(r'C:\Users\dimrizo\Desktop\PythonData\GTFS\routes.txt','w+',"UTF-8")

f3.write('route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color\n')
f3.write('blah,blah,blah,blah,blah,blah,blah,blah,blah\n')

问题是,如果我使用简单的 Windows 文本编辑器打开生成的文件,则文本的格式不正确。 "\n" 甚至不算数。如果我用崇高的文本打开文件,一切都很好,它应该被格式化。我应该怎么做才能在两个编辑器中看到格式正确的文本?

【问题讨论】:

  • 记事本无法正确处理\n。它必须是\r\n。

标签: python text output


【解决方案1】:

这是记事本本身的问题。它不能处理“Linux换行符”,而只能识别“windows newlinew”,所以你必须写\r\n,然后你会在记事本中看到换行符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2011-09-14
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    相关资源
    最近更新 更多