python中的in到底是什么意思是?

苦逼码农 2015-12-10 04:42:00
python中的in到底是什么意思是? 比如下面的:


# for line in urlopen('http://www.baidu.com'):
# line = line.decode('utf-8') # Decoding the binary data to text.
# if 'index' in line: # look for Eastern Time
# print(line)

line表示 http.client.HTTPResponse 对象集合中的中的一个元素吗?


type(urlopen(''))返回值说明类型是: http.client.HTTPResponse ,

该类并非列表, 也非元组,我不明白 for xx in http.client.HTTPResponse 里的每一个元素表示什么?

...全文
1181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2015-12-14
  • 打赏
  • 举报
回复

>>> str1 = "hello"
>>> str1
'hello'
>>> str2 = u"hello"
>>> str2
u'hello'
>>> bytes1 = bytearray(str1)
>>> bytes1
bytearray(b'hello')
>>> bytes2 = bytearray(str2)

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    bytes2 = bytearray(str2)
TypeError: unicode argument without an encoding
>>> bytes2 = bytearray(str2, 'utf')
>>> bytes2
bytearray(b'hello')
>>> 
这里如果只是英文,比较难看出差别。 建议了解下str,unicode和encode的知识。
ForestDB 2015-12-11
  • 打赏
  • 举报
回复
https://docs.python.org/2/reference/compound_stmts.html#the-for-statement =================================== http://python.usyiyi.cn/ Volunteers WANTED
苦逼码农 2015-12-11
  • 打赏
  • 举报
回复
引用 1 楼 dream_meng 的回复:
urlopen请求回来的返回值,是一个迭代器对象。 迭代器对象,是具有__next__()方法的对象。该方法能够自动返回下一个结果,当到达序列结尾时,引发StopIteration异常。 for ... in ...其实就是自动调用next方法,这里是自动取下一行内容
1. 你说异常,那怎么办? 需要try ... exception... finally不? 2. for ...in 到底有多少用法? 比如for ..in 一个序列(元组和list, string)可以逐一取出每个“元素” for ..ini 迭代器对象,又可以逐一调用next 还有没有其他用法? 一般的书籍讲解太少了,导致我猜测其用法,感觉这很不专业
苦逼码农 2015-12-11
  • 打赏
  • 举报
回复
引用 4 楼 ForestDB 的回复:
https://docs.python.org/2/reference/compound_stmts.html#the-for-statement =================================== http://python.usyiyi.cn/ Volunteers WANTED
已经搞懂, 顺便请教高手个问题: python 有unicode串的用法, 如 str1 = u'hello中' str2= 'hello中' 请问有什么区别? str1在内存中, 每个字符用 2个字节存储,如果不加,在内存中多少个字节?
Edward-liang 2015-12-10
  • 打赏
  • 举报
回复
楼上回答比较专业 我觉得通俗的说就是一种循环变量控制的语法 在所有变量中逐一取出。
方向H 2015-12-10
  • 打赏
  • 举报
回复
urlopen请求回来的返回值,是一个迭代器对象。 迭代器对象,是具有__next__()方法的对象。该方法能够自动返回下一个结果,当到达序列结尾时,引发StopIteration异常。 for ... in ...其实就是自动调用next方法,这里是自动取下一行内容

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧