我正在編寫一個代碼,它將文件文本作爲輸入,並執行以下操作: 它搜索我感興趣的特定行(在本例中爲'InChIKey=HCZNPUHZYPPINM-AWEZNQCLSA-M'
)並返回我行的利率之前出現行的索引+ 1,幷包含此字符'//'
Python在文本文件中找到感興趣的行的索引
這裏是我的代碼
file_info = open('compounds.dat', 'r')
data = {}
line_index = 0
unique_line_index = 0
for line in file_info:
if '//' in line:
#extract line index for lines that contain //
line_index = [x for x in range(len(content)) if "//" in content[x]]
elif 'InChIKey=HCZNPUHZYPPINM-AWEZNQCLSA-M' in line:
# return index of line after //
unique_line_index = line_index + 1
,但我得到了以下錯誤:can only concatenate list (not "int") to list
。
嗨,你可以分享compounds.dat和預期的結果? –
當你遇到''//''時''line_index'變成了一個列表,然後你做'line_index + 1'。錯誤信息不能更清楚。 – schwobaseggl