python:TypeError:无法将str写入文本流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4512982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:05:04  来源:igfitidea点击:

python: TypeError: can't write str to text stream

pythonio

提问by Jason S

I must be doing something obviously wrong here. But what is it, and how do I fix?

我一定在这里做错了什么。但它是什么,我该如何解决?

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> f1 = io.open('test.txt','w')
>>> f1.write('bingo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\appl\python.6.5\lib\io.py", line 1500, in write
    s.__class__.__name__)
TypeError: can't write str to text stream

edit: In my real application, I won't have a constant string, I'll have a regular string... if unicode is the issue, how do I convert to what io.open requires?

编辑:在我的实际应用程序中,我不会有一个常量字符串,我会有一个常规字符串......如果问题是 unicode,我如何转换为 io.open 需要的内容?

采纳答案by Edward Loper

The iomodule is a fairly new python module (introduced in Python 2.6) that makes working with unicode files easier. Its documentation is at: http://docs.python.org/library/io.html

io模块是一个相当新的 Python 模块(在 Python 2.6 中引入),可以更轻松地处理 unicode 文件。其文档位于:http: //docs.python.org/library/io.html

If you just want to be writing bytes (Python 2's "str" type) as opposed to text (Python 2's "unicode" type), then I would recommend you either skip the iomodule, and just use the builtin "open" function, which gives a file object that deals with bytes:

如果您只想写入字节(Python 2 的“str”类型)而不是文本(Python 2 的“unicode”类型),那么我建议您跳过该io模块,只使用内置的“open”函数,它给出一个处理字节的文件对象:

>>> f1 = open('test.txt','w')

Or, use 'b' in the mode string to open the file in binary mode:

或者,在模式字符串中使用 'b' 以二进制模式打开文件:

>>> f1 = io.open('test.txt','wb')

Read the docs for the io module for more details: http://docs.python.org/library/io.html

阅读 io 模块的文档以获取更多详细信息:http: //docs.python.org/library/io.html

回答by mbm

f = open("test.txt", "w")
f.write('bingo')
f.close()

equivalently,

等效地,

with open("test.txt", "w") as f:
    f.write('bingo')

and the termination of the block closes the file for you.

并且块的终止会为您关闭文件。

回答by user225312

Try:

尝试:

>>> f1.write(u'bingo')      # u specifies unicode

Reference

参考

回答by mipadi

Have you tried writing a Unicode string, instead of just a str? I.e.,

您是否尝试过编写 Unicode 字符串,而不仅仅是str? IE,

fq.write(u"bingo")

I'm on Mac OS X, but when I tried to write a str, I got the error

我在 Mac OS X 上,但是当我尝试编写一个 时str,出现错误

TypeError: must be unicode, not str

类型错误:必须是 unicode,而不是 str

Writing a Unicode string worked, though.

不过,编写一个 Unicode 字符串是可行的。

回答by Lennart Regebro

The io module differs from the old open in that it will make a big difference between binary and text files. If you open a file in text mode, reading will return Unicode text objects (called unicodein Python 2 and strin Python 3) and writing requires that you give it unicode objects as well.

io 模块与旧的 open 不同,它将在二进制文件和文本文件之间产生很大的不同。如果您以文本模式打开文件,读取将返回 Unicode 文本对象(unicode在 Python 2 和strPython 3 中调用),而写入也要求您为其提供 unicode 对象。

If you open in binary mode, you will get 8-bit sequential data back, and that's what you need to write. In Python 2 you use strfor this, in Python 3 bytes.

如果以二进制模式打开,将返回 8 位顺序数据,这就是您需要编写的内容。在 Python 2 中,您str用于此目的,在 Python 3 中bytes

You are using Python 2, and trying to write str to a file opened in text mode. That won't work. Use Unicode.

您正在使用 Python 2,并尝试将 str 写入以文本模式打开的文件。那行不通。使用 Unicode。