您的位置:首页 > 其它

Windows下生成txt文件时的换行

2017-07-03 09:19 239 查看
产品要个埋点的文件,于是写了个脚本处理埋点Key的枚举类,如下
import sys
import os
import string
import re

def replace(file_name):
file = open(file_name)
line_list = file.readlines()
new_line_list = []
pattern = re.compile('"(.*)"')
index = 0
while index <len(line_list):
line = line_list[index]
index += 1

kaValue = pattern.findall(line);
if len(kaValue) > 0:
line = pattern.findall(line)[0] + "\n"
else:
line = ""
new_line_list.append(line)
new_file = open("埋点.txt","wb")
new_file.writelines(new_line_list)

if __name__ == '__main__':
replace("DIYActionConstant.as")

其中,在匹配到埋点字段的每一行末尾加上了\n来换行。

输出结构在Sublime里面看到是正常的,但用记事本打开发现所有内容在一行。

然后度娘了一波:http://blog.csdn.net/xiaofei2010/article/details/8458605 http://stackoverflow.com/questions/1761051/difference-between-n-and-r





测试了一下:

测试1.




测试2.
#include <iostream>
using namespace std;

int main()
{
cout << "this is the first line\n";
cout << "this is the second line\r";
cout << "this is the third line\n";
cout << "this is the fouth line\r";
cout << "this is the fifth line\n";
cout << "end" ;
return 0;
}



测试2里面只使用\n就完成了换行,而按照之前文章中的说法和测试1的结果,应该只有\r\n才能完成换行,这个原因是:



所以使用标准输入输出流的时候用\n就能完成换行。

最后,关于为什么会有\r\n换行,度娘到的说法是:





https://books.google.com/books?id=ZNI5dvBSfZoC&pg=PA69&lpg=PA69&dq=Teletype+Model+33+%5Cr%5Cn&source=bl&ots=YrIPJvx9nu&sig=Aan02xRSJBKR4kuXmKKBrt1q7o4&hl=zh-CN&sa=X&ved=0ahUKEwi0mN6_oOnTAhUC9GMKHd0xCCgQ6AEINzAC#v=onepage&q=Teletype%20Model%2033%20%5Cr%5CnTeletype%20Model%2033&f=false

然后这台机器是1963年的。

噫,1963年的东西遗留到了现在,你也要洗半个世纪?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: