产品要个埋点的文件,于是写了个脚本处理埋点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换行,度娘到的说法是:
然后这台机器是1963年的。
噫,1963年的东西遗留到了现在,你也要洗半个世纪?