简体   繁体   中英

TypeError: expected a character buffer object in Python

I don't know why I can't write this document to file:

document=['', 'feeling frisky cool cat hits join us', 'askjoe app add music videos free today saintkittsandnevis', 'give dog flea bath midnight greeeeat smells like dawn', 'cat finds flash drive spent weeks looking', 'downside cat dude means can recognize nice smell pee second', 'louis pine apple make life brighter like star please follow me', 'gonna need nice cup coffee morning', 'gonna need nice cup coffee morning', 'iphone gives warning dies smh']

Here's the code for writing into the file:

with open("cleaned_tweet.txt", "w") as cleaned_tweet_file:
    cleaned_tweet_file.write(document)

Here's the error I get:

Traceback (most recent call last):
  File "test.py", line 85, in <module>
    cleaned_tweet_file.write(document)
TypeError: expected a character buffer object

The method write() takes as an argument a str and not a list object. This is the cause of your TypeError and wraping it in a str() call simply gives you the string representation of the list object documents and not the documents in the list per se.

By using:

with open("cleaned_tweet.txt", "w") as cleaned_tweet_file:
    cleaned_tweet_file.write(str(document))

You'll get the list object written as a string to your file, so a big string of:

"['', 'feeling frisky cool cat hits join us', 'askjoe app add music videos free today saintkittsandnevis', 'give dog flea bath midnight greeeeat smells like dawn', 'cat finds flash drive spent weeks looking', 'downside cat dude means can recognize nice smell pee second', 'louis pine apple make life brighter like star please follow me', 'gonna need nice cup coffee morning', 'gonna need nice cup coffee morning', 'iphone gives warning dies smh']"

will be present. Logically not what you're after.


Instead of going through the manual (and possibly erroneous) conversion of list to str you can either use file.writelines() which works with a list of strings, or file.write() in conjunction with a for loop:

With writelines() :

with open("cleaned_tweet.txt", "w") as cleaned_tweet_file:
    cleaned_tweet_file.writelines(document)

No need for a loop or manual conversion here.

With write and a simple for loop:

with open("cleaned_tweet.txt", "w") as cleaned_tweet_file:
    for tweet in document:
        cleaned_tweet_file.write(tweet)

This has the same exact effect. It's main advantage is that it allows you to specify additional content per line basis; if you need to add a newline per tweet and a prefix, you add it explicitly cleaned_tweet_file.write("Tweet: " + tweet + "\\n") .

Here's the solution, add str :

with open("cleaned_tweet.txt", "w") as cleaned_tweet_file:
    cleaned_tweet_file.write(str(document))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM