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