私はPythonの初心者にとって難しい任務を負っています.LaTexで書かれたソースファイルからテーブルをインポートする必要があります. テーブルの名前を識別子として使用し、テーブルの最初から最後まで、行ごとに配列に書き込むことを考えていました。この仕事をする「自然な」方法は何ですか?
1675 次
2 に答える
0
興味のある行の範囲を示すために、テーブルの最初と最後にラテックスコメントを個人的に入れます.
import linecache
FILEPATH = 'file.tex'
def get_line_range():
'returns the lines at which the table begins and ends'
begin_table_line = None
end_table_line = None
with open(FILEPATH, "r") as file:
array = []
for line_number, line in enumerate(file):
if 'latex comment denoting beginning of table' in line:
begin_table_line = line_number
if 'latex comment denoting end of table' in line:
end_table_line = line_number
return begin_table_line+1, end_table_line
def get_table():
'gets the lines containing the table'
start, end = get_line_range()
return [linecache.getline(FILEPATH, line) for line in xrange(start, end)]
上記のコードはテストなしで実行されましたが、.tex ファイルからテーブルを取得する必要があります。ただし、明らかな問題の 1 つは、ファイルを 2 回読み取り、確実に最適化できることです。
于 2015-12-11T14:48:53.457 に答える