0
私はPython 2.7を使用しています。標準モジュールのみを使用しています。すべてのリスト項目がPythonの添付項目に変更されます
私は、車のイベントに関する情報を含む巨大なデータファイルを持っています。個別の車のイベントタイプ(加速など)に関するデータをコピーして別のファイルに書き込もうとしています。私の現在のアプローチでは、別のファイルに書き込む前に、テーブルにデータを格納します(リスト内のリストを使用します)(下記の表の例を参照)。
Event Type | Data Location Start | Data Location End
--------------------------------------------
Accelerate | 99 | 181
Break | 182 | 263
Horn | 264 | 351
Accelerate | 352 | 434
...and so on
The table above in Python would be:
event_list = [['Accelerate', 99, 181],
['Break', 182, 263],
['Horn', 264, 351],
['Accelerate', 352, 434]]
問題:私は、行を追加するたびに、他のすべての行は、添付の行に変更します。私は以下のコードとコンソール出力を提供しています。
#!/usr/bin/python
""" File Description """
import os
def main():
""" Organize Car Data Into New File """
event_list = [] # This is the entire table
first_event = True # This is a flag
single_event = [-1, -1, -1] # This is a single row in the table
# [Event Name, Code Line Start, Code Line End]
with open('C:/car_event_data.dat', 'rb') as f:
line = '-1'
while line != '': # If line = '' then it is EOF
line = f.readline()
if line[0:6] == 'Event:':
if first_event == False:
single_event[2] = f.tell() - 1 # Code Line End
event_list.append(single_event) # Completed row
print(event_list)
end = line.find('\x03', 6) # Find the end location of Event Type
single_event[0] = line[6:end] # Event Type
single_event[1] = f.tell() # Code Line Start
first_event = False
f.seek(0, os.SEEK_END) # Put pointer at EOF
single_event[2] = f.tell()
event_list.append(single_event)
if __name__ == '__main__':
main()
上記のコードは次のような出力を生成する:
[['Accelerate', 99, 181]]
[['Break', 182, 263], ['Break', 182, 263]]
[['Horn', 264, 351], ['Horn', 264, 351], ['Horn', 264, 351]]
... and so on
ありがとうございました。これで問題は解決します –
ようこそ。答えを受け入れてください(緑色のチェックマークも)、担当者に役立ちます:) –