码迷,mamicode.com
首页 > 其他好文 > 详细

用with打开文件

时间:2018-04-28 11:43:06      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:ESS   pie   for   one   fill   pen   nothing   pre   打开文件   

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打开文件

标签:ESS   pie   for   one   fill   pen   nothing   pre   打开文件   

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

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