Python:打印时需要一个类似字节的对象,而不是 'str'
Python: a bytes-like object is required, not 'str' while printing
问题/我试过的方法
我下载了我尝试 运行 的 textmining 1.0
库,但是这给了我一些导入错误(因为这是一个 python 2 库)所以我在 Whosebug 上搜索并发现我必须使用 2to3.py
,现在一切正常。但是,当我这样做时:
def buildMatrix(self,document_list):
print("building matrix...")
tdm = textmining.TermDocumentMatrix()
for doc in document_list:
tdm.add_doc(doc)
tdm.write_csv(r'path\matrix.csv', cutoff=2)
(document_list 只是 strings
的列表)
我收到以下错误:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
f.writerow(row)
TypeError: a bytes-like object is required, not 'str'
在检查 textmining 1.0
的代码时,我很确定该行应该是 string
。所以我想通过编辑源代码来打印这一行:
f = csv.writer(open(filename, 'wb'))
for row in self.rows(cutoff=cutoff):
print(row)
f.writerow(row)
然而即使是现在我也得到了同样的结果TypeError
:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
print(row)
TypeError: a bytes-like object is required, not 'str'
我在堆栈溢出上进行了搜索,通过将 'wb'
替换为 'w'
来解决此问题,但这仍然给了我 TypeError.
问题
- 如何修复代码以使其能够写入行。
- 为什么打印语句也会导致
TypeError
根据评论编辑:
Claudio 的建议仍然给了我 TypeError
:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
f.write(row)
TypeError: a bytes-like object is required, not 'str'
托尼的建议:
代码检查:
for article in articles:
abstract = searcher.getArticleAbstract(article)
print(type(abstract)) #--> returns <class 'str'>
all_abstracts.append(abstract)
txtSearcher.buildMatrix(all_abstracts)
我现在有这些 open
行:
f = open(os.path.join(data_dir, 'stopwords.txt'),"r")
f = open(os.path.join(data_dir, 'dictionary.txt'),"r")
f = csv.writer(open(filename, 'w'))
发生了一些奇怪的事情
这将带我去:
def write_csv(self, filename, cutoff=2):
print("This really makes me sad!")
"""
Write term-document matrix to a CSV file.
filename is the name of the output file (e.g. 'mymatrix.csv').
cutoff is an integer that specifies only words which appear in
'cutoff' or more documents should be written out as columns in
the matrix.
"""
print(self.rows)
f = csv.writer(open(filename, 'w'))
for row in self.rows(cutoff=cutoff):
f.writerow(row)
它确实打印了 "building matrix.."(所以函数被调用)但是它不打印 print("This really makes me sad!")
据我目前所知,问题中描述的程序奇怪行为的实际原因是我在评论中提出的问题:
Are you sure that you are getting the error from the code you are editing?
Was not considered as relevant and the only right answer explaining all of the observed issues.
所有其他检测到的问题,例如
**RENAME** def write_csv(...) to for example def my_write_csv(...)
包括提供的解释和提示,例如:
如果您定义了一个与库中的函数同名的函数,您 运行 会遇到 local/global 作用域的麻烦,并且不知道实际执行了哪个函数?这个来自库或你定义的这个......你插入的 print("This really makes me sad!")
没有被打印这一事实表明不是这个函数被执行而是库函数被执行......
检查整个代码,包括要读取的文件或能够重现错误的摘录 - 对于这种奇怪的行为肯定有一个非常简单的解释。
在指示错误的行之前的代码中查找未闭合的括号或字符串引号或列表 ]
等。
在这种情况下无法成功...
使用更新的 textmining3 包代替 textmining 1
https://pypi.org/project/textmining3/
它将解决上述问题。
问题/我试过的方法
我下载了我尝试 运行 的 textmining 1.0
库,但是这给了我一些导入错误(因为这是一个 python 2 库)所以我在 Whosebug 上搜索并发现我必须使用 2to3.py
,现在一切正常。但是,当我这样做时:
def buildMatrix(self,document_list):
print("building matrix...")
tdm = textmining.TermDocumentMatrix()
for doc in document_list:
tdm.add_doc(doc)
tdm.write_csv(r'path\matrix.csv', cutoff=2)
(document_list 只是 strings
的列表)
我收到以下错误:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
f.writerow(row)
TypeError: a bytes-like object is required, not 'str'
在检查 textmining 1.0
的代码时,我很确定该行应该是 string
。所以我想通过编辑源代码来打印这一行:
f = csv.writer(open(filename, 'wb'))
for row in self.rows(cutoff=cutoff):
print(row)
f.writerow(row)
然而即使是现在我也得到了同样的结果TypeError
:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
print(row)
TypeError: a bytes-like object is required, not 'str'
我在堆栈溢出上进行了搜索,通过将 'wb'
替换为 'w'
来解决此问题,但这仍然给了我 TypeError.
问题
- 如何修复代码以使其能够写入行。
- 为什么打印语句也会导致
TypeError
根据评论编辑:
Claudio 的建议仍然给了我 TypeError
:
File "C:\Users\RICK\Anaconda\lib\site-packages\textmining\__init__.py", line 335, in write_csv
f.write(row)
TypeError: a bytes-like object is required, not 'str'
托尼的建议:
代码检查:
for article in articles:
abstract = searcher.getArticleAbstract(article)
print(type(abstract)) #--> returns <class 'str'>
all_abstracts.append(abstract)
txtSearcher.buildMatrix(all_abstracts)
我现在有这些 open
行:
f = open(os.path.join(data_dir, 'stopwords.txt'),"r")
f = open(os.path.join(data_dir, 'dictionary.txt'),"r")
f = csv.writer(open(filename, 'w'))
发生了一些奇怪的事情
def write_csv(self, filename, cutoff=2):
print("This really makes me sad!")
"""
Write term-document matrix to a CSV file.
filename is the name of the output file (e.g. 'mymatrix.csv').
cutoff is an integer that specifies only words which appear in
'cutoff' or more documents should be written out as columns in
the matrix.
"""
print(self.rows)
f = csv.writer(open(filename, 'w'))
for row in self.rows(cutoff=cutoff):
f.writerow(row)
它确实打印了 "building matrix.."(所以函数被调用)但是它不打印 print("This really makes me sad!")
据我目前所知,问题中描述的程序奇怪行为的实际原因是我在评论中提出的问题:
Are you sure that you are getting the error from the code you are editing?
Was not considered as relevant and the only right answer explaining all of the observed issues.
所有其他检测到的问题,例如
**RENAME** def write_csv(...) to for example def my_write_csv(...)
包括提供的解释和提示,例如:
如果您定义了一个与库中的函数同名的函数,您 运行 会遇到 local/global 作用域的麻烦,并且不知道实际执行了哪个函数?这个来自库或你定义的这个......你插入的 print("This really makes me sad!")
没有被打印这一事实表明不是这个函数被执行而是库函数被执行......
检查整个代码,包括要读取的文件或能够重现错误的摘录 - 对于这种奇怪的行为肯定有一个非常简单的解释。
在指示错误的行之前的代码中查找未闭合的括号或字符串引号或列表 ]
等。
在这种情况下无法成功...
使用更新的 textmining3 包代替 textmining 1
https://pypi.org/project/textmining3/
它将解决上述问题。