码迷,mamicode.com
首页 > 编程语言 > 详细

Python——处理文本换行符

时间:2016-08-08 12:55:55      阅读:543      评论:0      收藏:0      [点我收藏+]

标签:

源文件每行后面都有回车,所以用下面输出时,中间会多了一行

try:
    with open("F:\\hjt.txt" ) as f :
        for line in f:
            print(line)
except FileNotFoundError:
    print("读取文件出错")

 技术分享

有两种方法处理:

1.print后面带 end=‘‘,表示不换行

try:
    with open("F:\\hjt.txt" ) as f :
        for line in f:
            print(line,end=‘‘)
except FileNotFoundError:
    print("读取文件出错")

 

2.用strip()函数去掉每一行的换行符

try:
    with open("F:\\hjt.txt" ) as f :
        for line in f:
            line = line.strip(\n)
            print(line)
except FileNotFoundError:
    print("读取文件出错")

技术分享

 

Python——处理文本换行符

标签:

原文地址:http://www.cnblogs.com/hjhsysu/p/5748776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!