简体   繁体   English

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

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

I am new to python.我是python的新手。 I'm confused by the <class 'str'> .我对<class 'str'>感到困惑。 I got a str by using:我通过使用得到了一个 str:

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

The type of 'response' is <class 'str'> , not <type 'str'> . 'response' 的类型是<class 'str'> ,而不是<type 'str'> When I try to manipulate this str in 'for loop':当我尝试在“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”来获取我可以在“for循环”中使用的<type 'str'>字符串。

There is no difference.没有区别。 Python changed the text representation of type objects between python 2 ( Types are written like this: <type 'int'> . ) and python 3 ( Types are written like this: <class 'int'> . ). Python 在 python 2(类型写成这样: <type 'int'> . )和 python 3 (类型写成这样: <class 'int'> . )之间改变了type对象的文本表示。 In both python 2 and 3, the type of the type object is, um, type:在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:

response is a string and enumerating it gives the characters in the string. response是一个字符串,枚举它会给出字符串中的字符。 Depending on the type of response you may want to use and HTML, JSON or other parser to turn it into python objects.根据您可能想要使用的响应类型以及将其转换为 Python 对象的 HTML、JSON 或其他解析器。

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.至于遍历字符串,对字符串的 for 循环将逐个字符地遍历字符串。 If you want to iterate over each line in the string, you usually do something like split on \\n or some regex designed to split on the line separators in the URL response.如果您想遍历字符串中的每一行,您通常会执行诸如 split on \\n或一些旨在在 URL 响应中的行分隔符上拆分的正则表达式之类的操作。 Below is a simple for loop over the list resulting from split下面是一个简单的 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

In case you have the same confusing I got in Jupyter如果你有我在JupyterJupyter的同样困惑

Using type("hi") will give you str .使用type("hi")会给你str

While using print(type('hi')) will give you <class 'str'> Anyway, both are the same!虽然使用print(type('hi'))会给你<class 'str'>无论如何,两者都是一样的!

在此处输入图片说明

#python3 #python3

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

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