Python: byte object needed, not "str" ​​when printing

Problem / what I've tried

I downloaded the library textmining 1.0

I was trying to run, however it gave me some import errors (because it is a python 2 lib), so I searched in stackoverflow and found out what I had to use 2to3.py

and now everything works. However, when I do this:

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 is just a list strings

) I am getting the following error:

  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'

      

I'm pretty sure the line should be string

when checking the code textmining 1.0

. So I wanted to print this line by editing the source code:

f = csv.writer(open(filename, 'wb'))
        for row in self.rows(cutoff=cutoff):
            print(row)
            f.writerow(row)

      

However, even now I get the same thing 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'

      

I was looking for a stack overflow to solve this by replacing 'wb'

with 'w'

, however this still gives me TypeError.



Questions

  • How can I fix the code so that it can write a string.
  • Why does even the print operator call TypeError


Edit based on comment (s) :
Claudio's suggestion still gave me 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'

      

Tony's suggestion:
Checking the code:

for article in articles:
        abstract = searcher.getArticleAbstract(article)
        print(type(abstract)) #--> returns <class 'str'>
        all_abstracts.append(abstract)
    txtSearcher.buildMatrix(all_abstracts)

      

Now I have these lines 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'))

      

Some strange things are happening

enter image description here This takes me to:

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)

      

It prints the "construction matrix .." (that's why the function is called), however it doesn't print print("This really makes me sad!")

+3


source to share


1 answer


In my current knowledge, the actual reason that there was strange program behavior in the question in question was the fact that the question I asked in the comments:

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

Was not considered relevant and the only correct answer explaining all the problems found.

All other problems found, such as

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



including provided explanations and hints like:

If you define your own function with the same name as a function in the library, you run into problems with local / global scopes and are lost to know ANYTHING was actually executed? Is it one from the library or this that you defined ... The fact that the one you entered print("This really makes me sad!")

was not printed indicates that this function was not executed, but instead of the library ...

Check all the code, including the file read or the excerpts that can reproduce the error - there is a very simple explanation for this strange behavior.

Look for an unclosed parenthesis or string clause or list ]

, etc. in the code preceding the line containing the error.

could not lead to success in this case ...

0


source







All Articles