python读取文件,换行问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python读取文件,换行问题相关的知识,希望对你有一定的参考价值。

参考技术A 写法一:

写法二:

解析:

在 windows 系统中,路径最好用反斜杠 “\”。
路径也可以是全局路径如, “E:\test.txt” 之类

法一,使用 open(...) 函数的返回值 filename。
法二,用 filelines 列表先存储文件内容,再读取。

print()自带 换行。也就是默认带有 end='\n',Ctrl+鼠标点击可见函数原型为

所以,print时,若要去掉文件中自带的换行,line.rstrip() 去除空白字符。空白符(包括'\n', '\r', '\t', ' ')

当然,你也可以去掉默认的换行,而打印文件原样内容

去除字符串两边的空白符

去除字符串左边的空白符

去除字符串右边的空白符

python tips:文件读取——换行符的问题

问题:在windows系统中,换行的符号是‘ ‘。python在读文件的时候为了系统兼容,会默认把‘ ‘,‘n‘,‘ ‘都视作换行。但是在windows文件中,可能在同一行中同时存在‘ ‘,‘ ‘,‘ ‘。这个时候python的默认行为会将一行拆分成多行输出,影响预期结果。

此时需要设置open函数的newline参数,修改python对换行的默认行为。

open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

newline有五种取值:None,‘‘,‘ ‘,‘ ‘,‘ ‘。

在输入过程(从文件到程序),newline用于定义换行的符号:

1.如果newline为None,碰到‘ ‘,‘ ‘,‘ ‘都算行尾,而且这些符号都会被转换成‘ ‘。

2.如果newline为‘‘,也是碰到‘ ‘,‘ ‘,‘ ‘都算行尾,但是这些符号不会发生转换。

3.如果newline为‘ ‘,‘ ‘,‘ ‘,等于是显示指定了换行符,而且行中的符号不会发生转换。

在输出过程(从程序到文件),newline用于指定‘ ‘的转换符号:

1.如果newline为None,所有的‘ ‘都被转换成系统换行符。

2.如果newline为‘‘,‘ ‘,不会发生转换。

3.如果newline为‘ ‘,‘ ‘,所有的‘ ‘会被转换成‘ ‘或者‘ ‘。

实例一:输出不指定newline,所有的‘ ‘都被替换成‘ ‘,即使是‘ ‘中的‘ ‘也不例外。

def file_seperator_test1():
    # output
    with open("medical.txt", "w") as f:
        f.write("I am a
 good
 boy.
")
    #input
    with open("medical.txt", "r", newline="
") as f:
        print(list(f))

if __name__ == "__main__":
    file_seperator_test1()

输出结果:

[‘I am a
 good
‘, ‘ boy.

‘]

实例二: 输出指定newline为‘‘或‘ ‘,不会转换

def file_seperator_test2():
    # output
    with open("medical.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
")
    with open("medical2.txt", "w", newline="
") as f:
        f.write("I am a
 good
 boy.
")

    #input
    with open("medical.txt", "r", newline="
") as f:
        print(list(f))
    with open("medical2.txt", "r", newline="
") as f:
        print(list(f))

if __name__ == "__main__":
    file_seperator_test2()

输出结果:

[‘I am a
 good
 boy.
‘]
[‘I am a
 good
 boy.
‘]

实例三:输出指定newline为‘ ‘或‘ ‘,所有的‘ ‘都被替换了,当所有‘ ‘都被替换成‘ ‘时,在windows中,换行符就不见了,所有的行变成了一行

def file_seperator_test3():
    # output
    with open("medical.txt", "w", newline="
") as f:
        f.write("I am a
 good
 boy.
 where should
 I change the line ?
")
        f.write("I can‘t stop
")
    with open("medical2.txt", "w", newline="
") as f:
        f.write("I am a
 good
 boy.
")

    #input
    with open("medical.txt", "r", newline="
") as f:
        print(list(f))
    with open("medical2.txt", "r", newline="
") as f:
        print(list(f))


if __name__ == "__main__":
    file_seperator_test3() 

输出结果:

["I am a
 good
 boy.

 where should

 I change the line ?

I can‘t stop

"]
[‘I am a
 good
‘, ‘ boy.

‘]

实例四:输入不指定newline,默认把所有的三种符号都当做换行符,而且全都转换成‘ ‘

def file_seperator_test4():
    # output
    with open("medical.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
")
    #input
    with open("medical.txt", "r") as f:
        print(list(f))


if __name__ == "__main__":
    file_seperator_test4() 

输出结果:

[‘I am a
‘, ‘ good
‘, ‘ boy.
‘]

实例五:输入指定newline为‘‘,仍然把三种符号都当做换行符,但是不转换

def file_seperator_test5():
    # output
    with open("medical.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
")
    #input
    with open("medical.txt", "r", newline="") as f:
        print(list(f))

if __name__ == "__main__":
    file_seperator_test5()

输出结果:

[‘I am a
‘, ‘ good
‘, ‘ boy.
‘]

实例六:输入指定newline为‘ ‘,‘ ‘,‘ ‘,显式指定了换行符,只有碰到这几个符号才会换行

def file_seperator_test6():
    # output
    with open("medical.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
 where should
 I change the line ?
")
        f.write("I can‘t stop
")
    with open("medical2.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
 where should
 I change the line ?
")
        f.write("I can‘t stop
")
    with open("medical3.txt", "w", newline="") as f:
        f.write("I am a
 good
 boy.
 where should
 I change the line ?
")
        f.write("I can‘t stop
")

    #input
    with open("medical.txt", "r", newline="
") as f:
        print(list(f))
    with open("medical2.txt", "r", newline="
") as f:
        print(list(f))
    with open("medical3.txt", "r", newline="
") as f:
        print(list(f))

if __name__ == "__main__":
    file_seperator_test6()

输出结果:

[‘I am a
‘, ‘ good
 boy.
‘, ‘
 where should
‘, ‘
 I change the line ?
‘, "
I can‘t stop
", ‘
‘]
[‘I am a
 good
‘, ‘ boy.
‘, ‘ where should
‘, ‘ I change the line ?
‘, "I can‘t stop
"]
[‘I am a
 good
 boy.
‘, ‘ where should
‘, ‘ I change the line ?
‘, "I can‘t stop
"]

结论:

1.如果要写入带‘ ‘的行,可以把newline设定为‘‘或者‘ ‘,避免python更改‘ ‘

2.如果要读入带‘ ‘的行,可以把newline设定为‘ ‘,指定换行符只能是‘ ‘。

 

以上是关于python读取文件,换行问题的主要内容,如果未能解决你的问题,请参考以下文章

python按行读取文件,如何去掉换行符"\n

python按行读取文件,如何去掉换行符"\\n

python按行读取文件,如何去掉换行符"\n

python按行读取文件,如何去掉换行符"\n

python 按每行读取文件怎么去掉换行符

有关Java 文件读取中换行的问题