2

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

2 Answers 2

5

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").

Sign up to request clarification or add additional context in comments.

3 Comments

so is using str better or a for loop as shown by you?
Using write(str(documents)) will write the whole list including brackets [] to the file. I don't think you want that. I would suggest writelines in this case unless you need each item in your list documents on a new line for which I would suggest using the for loop with the command cleaned_tweet_file.write(tweet + "\n").
You're welcome Mona, take a look at the urls I added from the documentation. They're really helpful in understanding these calls.
3

Here's the solution, add str:

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

1 Comment

There were a few comments in IRC after I showed them this solution that this might cause further problems. So if you have a better solution please provide it, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.