【发布时间】:2019-02-15 05:14:27
【问题描述】:
每次我尝试拆分一个文件的内容并将它们写入另一个文件时都会收到类型错误。
该错误表明应该使用字符串而不是列表,因此我尝试先对其进行转换,但仍然收到相同的错误。该文件是一个去除了 html 标签的 html 页面,因此它只是纯文本
这是收到的错误
...............line 30, in split
w.write(re.split('[^\.\!\?]*[\.\!\?]',str(str1)))
TypeError: write() argument must be str, not list
这是python函数
def split():
x=open("file_a.txt")
w= open("file_b.txt","w+")
str1= x.readlines()
w.write(re.split('[^\.\!\?]*[\.\!\?]',str(str1)))
【问题讨论】:
-
split的输出是一个列表。然后您尝试write列表,但“写列表”是什么意思?会不会是[1, 2, 3],就像 Python 会写它一样?或1<newline>2<newline>3<newline>,每行一项?也许只是把元素粘在一起,比如123?用1, 2 and 3加英文吗?具体来说,当您说“拆分一个文件的内容并将它们写入另一个文件”时,您想要什么?
标签: python regex python-3.x nltk