简体   繁体   English

TypeError:应为Python中的字符缓冲区对象

[英]TypeError: expected a character buffer object in Python

I don't know why I can't write this document to file: 我不知道为什么无法将此document写入文件:

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. write()方法write() str而不是list对象作为参数。 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. 这是导致TypeError的原因,将其包装在str()调用中只是给您 list对象documents 的字符串表示形式,不是列表本身的字符串表示形式

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: 您将获得以字符串形式写入文件的list对象,因此有一个很大的字符串:

"['', '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: 可以通过将file.writelines()与字符串list一起使用,或者将file.write()for循环结合使用,而不是将list手动(也可能是错误地)将list转换为str

With writelines() : 使用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: 使用write和一个简单的for循环:

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") . 如果您需要为每个tweet添加一个换行符和一个前缀,则可以显式添加它cleaned_tweet_file.write("Tweet: " + tweet + "\\n")

Here's the solution, add str : 这是解决方案,添加str

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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