Прежде всего, вы можете немного сократить свой код, удалив ненужные объявления.
Редактировать: Полагаю, вы хотите открыть csv_name
, а не "atoms.csv"
?
С удалением всего устаревшего, это выглядит так (посмотрите здесь для получения дополнительной информации по csv.reader
):
def compound_properties(csv_name, compound_formula):
compDict = molform(compound_formula)
with open(csv_name, "r") as atomsF:
elemData = csv.reader(atomsF) # csv.reader already returns a list of rows
for i in compDict: # for loop automaticaly iterates over dict keys
for j in elemData:
if str(i) in j: # no need to assign i or j to additional variables
print('hi')
Без каких-либо реальных примеров данных я не могу сейчас сказать больше об этом, потому что проблема не в этом цикле for. С тестовыми данными он отлично работает:
elem_data = [['this', 'is', 'the', 'first', 'row'], ['this', 'is', 'the', 'bar', 'row'], ['this', 'is', 'all', 'a', 'big', 'foo']]
compDict = {'foo': 1, 'bar': 2, 'baz': 3}
for i in compDict:
for j in elemData:
if str(i) in j:
print('{} was found in line {}'.format(i, elemData.index(j) + 1))
Выход:
foo was found in line 3
bar was found in line 2