Windows下生成txt文件时的换行



产品要个埋点的文件,于是写了个脚本处理埋点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年的东西遗留到了现在,你也要洗半个世纪?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值