Python <class 'str'> 和 <type 'str'> 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42048107/
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-19 21:10:05  来源:igfitidea点击:

What is the difference between <class 'str'> and <type 'str'>

pythontypesdecodeurllib

提问by dom free

I am new to python. I'm confused by the <class 'str'>. I got a str by using:

我是python的新手。我对<class 'str'>. 我通过使用得到了一个 str:

response = urllib.request.urlopen(req).read().decode()

The type of 'response' is <class 'str'>, not <type 'str'>. When I try to manipulate this str in 'for loop':

“响应”的类型是<class 'str'>,不是<type 'str'>。当我尝试在“for 循环”中操作此 str 时:

for ID in response: 

The 'response' is read NOT by line, BUT by character. I intend to put every line of 'response' into individual element of a list. Now I have to write the response in a file and use 'open' to get a string of <type 'str'>that I can use in 'for loop'.

“响应”不是按行读取,而是按字符读取。我打算将每一行“响应”放入列表的单个元素中。现在我必须将响应写入文件并使用“open”来获取<type 'str'>我可以在“for循环”中使用的字符串。

采纳答案by 2ps

As mentioned by the commenters. In python3:

正如评论者所说。在python3中:

>>>st = 'Hello Stack!'
>>>type(st)
<class 'str'>

But in python2:

但是在python2中:

>>>st = 'Hello Stack!'
>>>type(st)
<type 'str'>

So the behavior that you are seeing is entirely expected. As to looping over a string, a for loop over a string will iterate over the string character by character. If you want to iterate over each line in the string, you usually do something like split on \nor some regex designed to split on the line separators in the URL response. Below is a simple for loop over the list resulting from split

因此,您所看到的行为完全符合预期。至于遍历字符串,对字符串的 for 循环将逐个字符地遍历字符串。如果您想遍历字符串中的每一行,您通常会执行诸如 split on 之类的操作,\n或者一些旨在拆分 URL 响应中的行分隔符的正则表达式。下面是一个简单的 for 循环遍历列表产生的结果split

response = urllib.request.urlopen(req).read().decode()
lines = response.split('\n')
for x in lines:
    st = x.strip()
    # do some processing on st

回答by tdelaney

There is no difference. Python changed the text representation of typeobjects between python 2 (Types are written like this: <type 'int'>.) and python 3 (Types are written like this: <class 'int'>.). In both python 2 and 3, the type of the type object is, um, type:

没有区别。Pythontype在 Python 2(类型写成这样:<type 'int'>.)和 Python 3(类型写成这样:<class 'int'>.)之间改变了对象的文本表示。在python 2和3中,类型对象的类型都是,嗯,类型:

python 2

蟒蛇2

>>> type(type('a'))
<type 'type'>

python 3

蟒蛇 3

>>> type(type('a'))
<class 'type'>

And that's the reason for the change... the string representation makes it clear that the type is a class.

这就是改变的原因……字符串表示清楚地表明类型是一个类。

As for the rest of your problem,

至于你剩下的问题,

for ID in response:

responseis a string and enumerating it gives the characters in the string. Depending on the type of response you may want to use and HTML, JSON or other parser to turn it into python objects.

response是一个字符串并枚举它给出字符串中的字符。根据您可能想要使用的响应类型以及将其转换为 Python 对象的 HTML、JSON 或其他解析器。