簡體   English   中英

逐行讀取文件會跳過每一行

[英]Reading file line-by-line skips every second line

為什么這段代碼會打印原始文件中的備用字符串,因為您可以看到索引號從 0-2-4-6 開始?

kanji_file = open('kanji.txt', 'r', encoding="utf8")

while kanji_file.readline() != '':

    print(kanji_file.readline(), end='')

kanji_file.close()

output:

0       ああ            int     Ah!, Oh!
2       あお    青      n       blue
4       あか    赤      n       red
6       あかるい        明るい  adj     bright, cheerful
8       あく    開く    u-v,vi  to open, to become open 
10      あげる  上げる  ru-v    to give
12      あさごはん      朝御飯  n       breakfast
14      あし    足      n       foot, leg
16      あそこ          n       there, over there, that place
18      あたたかい      暖かい  adj     warm, mild
20      あたらしい      新しい  adj     new
22      あつい  暑い    adj     hot, warm
24      あつい  厚い    adj     kind, warm(hearted), thick, deep
26      あと    後      adj-no,n        afterwards, since then, in the future
28      あに    兄      n       (hum) older brother
30      あの            pren-adj        that over there
32      アパート                n,adv   apartment (abbr)
34      あぶない        危ない  adj     dangerous, critical, watch out!
36      あまり          na-adj,adv,n,n-suf      not very, not much
38      あめ    飴      n       (hard) candy, toffee
40      ある [存在]             u-v-i   to be
42      あるく  歩く    u-v     to walk
44      いい/よい               adj     good
46      いう    言う    u-v     to say
48      いかが          na-adj,adv,n    how, in what way
50      いくつ          n       how many?, how old?
52      いけ    池      n       pond
54      いす            n       chair
56      いたい  痛い    adj     painful
58      いちにち        一日    n       1 day (duration)
60      いつ            gn-adv  when
62      いっしょ        一緒    adv,n   together
64      いつも          adv,n   always, every time
66      いま    今              now
68      いもうと        妹      n       younger sister
70      いりぐち        入口    n       entrance, gate
72      いる    要る    u-v     to need
74      いろ    色      n       colour
76      うえ    上      no-adj,n-adv,n,n-suf    above, on top of
78      うすい  薄い    adj     thin, weak
80      うたう  歌う    u-v     to sing
82      うまれる        生まれる        ru-v    to be born
84      うる    売る    u-v     to sell
86      うわぎ  上着    n       coat, jacket
88      えいが  映畫    n       movie, film
90      えいご  英語    n       the English language
92      えき    駅      n       station
94      ~えん  ~円    n       yen (currency)
96      お~            prefix  honourable ~ (honorific)
98      おおい  多い    adj     many
100     おおきな        大きな  na-adj  big

原始文件內容:

#   Kana    Kanji   Type    Definition/s
0   ああ      int Ah!, Oh!
1   あう  會う  u-v to meet, to see
2   あお  青   n   blue
3   あおい 青い  adj blue
4   あか  赤   n   red
5   あかい 赤い  adj red
6   あかるい    明るい adj bright, cheerful
7   あき  秋   n-adv   autumn, fall
8   あく  開く  u-v,vi  to open, to become open
9   あける 開ける ru-v    to open
10  あげる 上げる ru-v    to give
11  あさ  朝   n-adv,n-t   morning
12  あさごはん   朝御飯 n   breakfast
13  あさって        n-adv,n-t   day after tomorrow
14  あし  足   n   foot, leg
15  あした     n-t tomorrow
16  あそこ     n   there, over there, that place
17  あそぶ 遊ぶ  u-v to play, to enjoy oneself
18  あたたかい   暖かい adj warm, mild
19  あたま 頭   n   head
20  あたらしい   新しい adj new
21  あちら     n   there, yonder, that
22  あつい 暑い  adj hot, warm
23  あつい 熱い  adj hot (thing)
24  あつい 厚い  adj kind, warm(hearted), thick, deep
25  あっち     n col   over there
26  あと  後   adj-no,n    afterwards, since then, in the future
27  あなた     n   you
28  あに  兄   n   (hum) older brother
29  あね  姉   n   (hum) older sister
30  あの      pren-adj    that over there
31  あの      int um...
32  アパート        n,adv   apartment (abbr)
33  あびる     ru-v    to bathe, to shower
34  あぶない    危ない adj dangerous, critical, watch out!
35  あまい 甘い  adj,expr    generous, sweet
36  あまり     na-adj,adv,n,n-suf  not very, not much
37  あめ  雨   n   rain
38  あめ  飴   n   (hard) candy, toffee
39  あらう 洗う  u-v to wash
40  ある [存在]     u-v-i   to be
41  ある [所有]     u-v-i   to have
42  あるく 歩く  u-v to walk
43  あれ      int,n   that, that thing
44  いい/よい       adj good
45  いいえ     int,n   no, not at all
46  いう  言う  u-v to say
47  いえ  家   suf house, family
48  いかが     na-adj,adv,n    how, in what way
49  いく/ゆく   行く  u-v to go
50  いくつ     n   how many?, how old?
51  いくら     adv,n   how much?, how many?
52  いけ  池   n   pond
53  いしゃ 醫者  n   doctor (medical)
54  いす      n   chair
55  いそがしい   忙しい adj busy, irritated
56  いたい 痛い  adj painful
57  いち  一   num one
58  いちにち    一日  n   1 day (duration)
59  いちばん        n-adv   best, first, number one
60  いつ      gn-adv  when
61  いつか 五日  n   five days, the fifth day (of the month)
62  いっしょ    一緒  adv,n   together
63  いつつ 五つ  n   five
64  いつも     adv,n   always, every time
65  いぬ  犬       dog
66  いま  今       now
67  いみ  意味  n,vs    meaning
68  いもうと    妹   n   younger sister
69  いや  嫌   na-adj,n    disagreeable, no
70  いりぐち    入口  n   entrance, gate
71  いる  居る  u-v (hum) to be (animate), to exist
72  いる  要る  u-v to need
73  いれる 入れる ru-v    to put in
74  いろ  色   n   colour
75  いろいろ        na-adj,adj-no,adv,n various
76  うえ  上   no-adj,n-adv,n,n-suf    above, on top of
77  うしろ 後ろ  n   behind, rear
78  うすい 薄い  adj thin, weak
79  うた  歌   n   song
80  うたう 歌う  u-v to sing
81  うち      n   house (one's own)
82  うまれる    生まれる    ru-v    to be born
83  うみ  海   n   sea, beach
84  うる  売る  u-v to sell
85  うるさい        adj noisy, loud, annoying
86  うわぎ 上着  n   coat, jacket
87  え   絵   n,n-suf picture, drawing, painting, sketch
88  えいが 映畫  n   movie, film
89  えいがかん   映畫館 n   movie theatre (theater), cinema
90  えいご 英語  n   the English language
91  ええ      conj,int,n  yes
92  えき  駅   n   station
93  エレベーター      n   elevator
94  ~えん ~円  n   yen (currency)
95  えんぴつ    鉛筆  n   pencil
96  お~      prefix  honourable ~ (honorific)
97  おいしい        adj delicious, tasty
98  おおい 多い  adj many
99  おおきい    大きい adj big
100 おおきな    大きな na-adj  big

這是另一種讀取文件的方法,可能會給您帶來更好的結果:

with open('kanji.txt', 'r', encoding="utf8") as f:
    for line in f.read():
        print(line)

您以前的方法的問題是您正在讀取 while 循環定義和循環本身中的行。

在循環的每次迭代之后,檢查 while 循環的表達式以查看它是否仍然為真,它讀取一行而不打印它,將讀取的 cursor 向前移動一行。 該過程在循環本身內重復。

我概述的方法的另一個優點是,一旦with塊中的代碼完成,文件句柄就會自動關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM