Python,将关键字之间的txt行附加到另一个txt

时间:2017-05-15 10:31:09

标签: python string file copy save

此处不是有经验的用户。

我有一个文本文件(export.txt):

*NODE
several thousand lines like this one
several thousand lines like this one
several thousand lines like this one
*ANY OTHETR WORD

我想将这两个关键字之间的行(不幸地以" *"开头)复制到另一个文件(original.txt)的末尾。

尝试this solution,但在尝试复制数据时遇到错误:

* * * "Traceback (most recent call last):
File "myscript.py", line 28, in <module>
for line in f:
File "/opt/ansa_15.2.3/meta_post_v15.2.3/../shared_v15.2.3/python/linux64/lib/python3.3/codecs.py",
line 300, in decode (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 267: invalid continuation byte"
* * *

可以要求解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:0)

http://docs.python.org/howto/unicode.html#the-unicode-type

str = unicode(str, errors='replace')

OR

str = unicode(str, errors='ignore')

注意:此解决方案将删除(忽略)有问题的字符,返回没有它们的字符串。只有在需要剥离它们而不转换它们时才使用它。

或者,使用编解码器模块中的open方法读入文件:

import codecs
with codecs.open(file_name, "r",encoding='utf-8', errors='ignore') as fdata: