【问题标题】:Python: a bytes-like object is required, not 'str' while printingPython:需要一个类似字节的对象,而不是打印时的“str”
【发布时间】:2017-09-20 19:55:25
【问题描述】:

问题/我尝试了什么

我下载了我尝试运行的 textmining 1.0 库,但是这给了我一些导入错误(因为这是一个 python 2 库)所以我在 stackoverflow 上搜索并发现我必须使用 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

根据评论编辑
克劳迪奥的建议还是给了我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)

它确实打印了“构建矩阵..”(因此调用了该函数)但是它不打印print("This really makes me sad!")

【问题讨论】:

  • 在 Python 3 中,您应该使用带有 csv writer 的文本模式,以及 newline=''。至于为什么print()会出现同样的错误,是不是被什么东西遮住了?

标签: python csv export-to-csv text-mining


【解决方案1】:

据我目前所知,问题中描述的程序奇怪行为的实际原因是我在 cmets 中提出的问题:

Are you sure that you are getting the error from the code you are editing?

不被认为是相关的,并且是解释所有观察到的问题的唯一正确答案。

所有其他检测到的问题,例如

**RENAME** def write_csv(...) to for example def my_write_csv(...)

包括提供的解释和提示,例如:

如果您定义了一个与库中的函数同名的自己的函数,您会遇到本地/全局范围的问题,并且不知道实际执行了哪个函数?库中的这个或者你定义的这个......事实上,你插入的print("This really makes me sad!")没有被打印出来,这表明没有执行这个函数,而是库的一个......

检查整个代码,包括要读取的文件或能够重现错误的摘录 - 对于这种奇怪的行为肯定有一个非常简单的解释。

在指示错误的行之前的代码中查找未闭合的括号或字符串引号或列表] 等。

在这种情况下无法成功...

【讨论】:

  • 它仍然给出同样的错误(见我的问题编辑)
  • 发布或提供整个代码......问题的原因不是你看它的地方......也许你运行了错误的文件???
  • CSV 写入器对象是否有方法write()
  • print(dir(row)) 也是?您确定您正在编辑的代码中出现错误吗?
  • 如果我将其更改为 my_write_csv 并使用“转到定义”,它将带我进入已编辑的函数,但是运行代码会给我“'TermDocumentMatrix' 对象没有属性 'my_write_csv'”
【解决方案2】:

使用更新的 textmining3 包而不是 textmining 1

https://pypi.org/project/textmining3/

它将解决上述问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多