Python 输出一个 .txt 文件,其格式因我用来打开它的文本编辑器而异

Python outputs a .txt file, which's format differs depending on the text editor I use to open it

所以我有一些 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" 甚至不算数。如果我用 sublime text 打开文件,一切都很好,它的格式是应该的。我应该怎么做才能在两个编辑器中看到格式正确的文本?

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