1

約10,000行のテキストファイルがあります。
典型的な行は次のようになります。

'1 2/1/2011 9:30,ZQZ,200.02,B,500'

#1を実行すると、ファイル全体を反復処理でき、ファイルiの合計行数をカウントします。ただし、ファイルを反復処理するときに各行のデータを記録する辞書を作成すると(#2のように)、途中で取得できます。なぜこれが起こっているのか理解できません。10,000行のデータが大きすぎて辞書に含めることができない可能性はありますか?どうすればこれを判断できますか?#1 TheFile = open(file_name)TheFile.next()

i = 0
for l in TheFile:
   i += 1
   print i

#2
TheFile = open(file_name)
TheFile.next()
thedata = {}
i = 0
for l in TheFile:
   i += 1
   print i
   this_line = TheFile.next()
   the_info = this_line.split(',')
   the_ticker = the_info[1]
   #print type(the_info[1])
   #print this_line
   if the_ticker not in thedata.keys():
      thedata[the_ticker] = {}

   thedata[the_ticker]['trade'+ str(len(thedata[the_ticker]) + 1)] =
   {'the_trade_number':len(thedata[the_ticker]),
    'theTime':the_info[0],
    'thePrice':float(the_info[2]),
    'theTransaction':the_info[3],
    'theQuantity':int(the_info[4])}

問題は、#2でエラーが発生しないことです。そのため、問題が何であるかを理解するのに苦労しています。

4

1 に答える 1

2

あなたの問題は、実行#2にあります:

for l in TheFile:
   i += 1
   print i
   this_line = TheFile.next()

lには既に現在の行があり、 を使用して別の行を取得しTheFile.next()ます。に変更this_line = TheFile.next()するとthis_line = l、期待どおりの結果が得られるに違いありません。

于 2012-12-02T23:11:38.533 に答える