for i in xrange(0,5)使用過程當中遇到的問題

文件中共有4行內容。3d

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 "-----"file

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

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

-----
<type 'str'>top

-----
<type 'str'>db

-----
<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不換行的方法,後面加逗號","

相關文章
相關標籤/搜索