添加到当前文本文件然后逐行显示新文本?

时间:2015-12-01 00:05:19

标签: python-3.x

path = "c:\\temp\\mypythonfiles\\plague_warning.txt"
txt = open(path, "r+")

for line in txt:
    print(line)

txt.write("The Black Death was one of the most devastating\n")
txt.write("pandemics in human history, resulting in the deaths\n")
txt.write("of an estimated 75 to 200 million people and peaking\n")
txt.write("in Europe in the years 1346–53. Although there were\n")
txt.write("several competing theories as to the etiology of the\n")
txt.write("Black Death, analysis of DNA from victims in northern\n")
txt.write("and southern Europe published in 2010 and 2011 indicates\n")
txt.write("that the pathogen responsible was the Yersinia pestis\n")
txt.write("bacterium, probably causing several forms of plague.\n")

txt.close()


path = "c:\\temp\\mypythonfiles\\plague_warning.txt"
txt = open(path, "r")


for line in txt:
    print(line)

txt.close()

正确显示原始.txt文档,并将新行正确附加到文档中。下面是脚本显示附加文档的前几行后收到的错误。它通过新写入文本的第三行将其显示为峰值。

追踪(最近一次通话):   文件" e:\ pythonscripts \ Lesson12_2.py",第45行,in     打印(行)   文件" C:\ Python34 \ lib \ encodings \ cp437.py",第19行,编码     return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError:' charmap'编解码器不能编码字符' \ u2013'在位置 27:角色映射到

0 个答案:

没有答案