for i in xrange(0,5)使用过程中遇到的问题


文件中共有4行内容。

fd = open("C:\Users\william\Desktop\dup_file - Copy (2).txt")
for i in xrange(0,5):
print type(fd.readline())
print fd.readline()
print "-----"

<type 'str'>
ccf30309-2a02-4d09-8ea8-70c3da8de09d

-----
<type 'str'>
9cee8472-dfc1-49ac-90c3-572f68373934

-----
<type 'str'>

-----
<type 'str'>

-----
<type 'str'>

-----

执行第1个print,返回了第1行的type,然后第2个print,返回了第2行的value,并print后面有换行符

后面循环,返回了第3行的type, 然后是第4行的value。

所以这里要注意的是,每次循环,最好注意调用readline的次数。

 

fd = open("C:\Users\william\Desktop\dup_file - Copy (2).txt")
for i in xrange(0,5):
print i
print fd.readline()
print "-----"

 

0
8957d983-05e2-4a54-a7b3-f1532421c7a7

-----
1
ccf30309-2a02-4d09-8ea8-70c3da8de09d

-----
2
492ddb07-e0c5-4dda-ae91-8b3700d05a79

-----
3
9cee8472-dfc1-49ac-90c3-572f68373934

-----
4

-----

 

fd = open("C:\Users\william\Desktop\dup_file - Copy (2).txt")
for i in xrange(0,5):
print i
print fd.readline(),

 

0
8957d983-05e2-4a54-a7b3-f1532421c7a7
1
ccf30309-2a02-4d09-8ea8-70c3da8de09d
2
492ddb07-e0c5-4dda-ae91-8b3700d05a79
3
9cee8472-dfc1-49ac-90c3-572f68373934
4

这里主要是复习一下print不换行的方法,后面加逗号","


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM