首页 > 其他 > 详细

用with打开文件

时间:2018-04-28 11:10:24      阅读:199      评论:0      收藏:0      [点我收藏+]
rep_word = ‘The piece is gone, left the puzzle undone‘

# \ 换行,跟shell一样
with open(‘nothing‘, ‘r‘, encoding=‘utf-8‘) as f,     open(‘nothing1‘, ‘w‘, encoding=‘utf-8‘) as f_new:
        for line in f:
            if rep_word in line:
                line = line.replace(rep_word, ‘碎片散去,留下已解的迷‘)
            f_new.write(line)


# 运行前 nothing
# Trying hard to fill the emptiness
# The piece is gone, left the puzzle undone
# Ain‘t that the way it is

# 运行后 nothing1
# Trying hard to fill the emptiness
# 碎片散去,留下已解的迷
# Ain‘t that the way it is

  

用with打开文件

原文:https://www.cnblogs.com/PAYNE1Z/p/8966354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!