Я пытаюсь заполнить рабочий лист, но снова застрял. Мне нужно сохранить все комментарии Mooneye Studios в списке, который обновляется каждый раз, когда появляется новый комментарий от создателя проекта. Я пытаюсь сделать это с помощью первых двух условий в цикле for. Я получаю moon = [\ n, \ n, \ n ....]. Любая идея, как действовать и делать эту работу? Файл выглядит следующим образом: http://imgur.com/bsSr06q.Создание списка из .txt с использованием цикла for
comments = open('lostEmberComments.txt', 'r')
nbOfCom = 0 #counting the commentaries
people = []# list of ppl who have commented
creator = False # did the creator write the comment?
moon = [] # lists of comments made by the creator
temp= ''
for line in comments:
# my attempt
if '>>> Mooneye Studios' in line:
creator = True
if creator and '>>>' not in line:
temp += line
if '>>>' and 'Mooneye Studios' not in line:
creator = False
if temp != '':
moon.append(temp)
temp = ''
# this section is for the first part of the excercise
if '>>>' in line:
nbOfCom += 1 # counting the commentaries
if not line.rstrip('\n') in people: # avoiding duplicates
people.append(line.rstrip('\n'))
b = len(people)
print(moon)
print('Discussion participants: ', people)
print('There are ', b, 'contributors to the discussion.')
print('There are ',nbOfCom,' commentaries on this project.')
comments.close()
\ п является новой строки в Linux, и можно использовать line.replace («\ п», «») – Wonka
Предполагается, что если творец в строке и >>> не в строке. –
Я не считаю следующее условие: 'if '>>>' и 'Mooneye Studios' не в строке:' делает то, что вы, возможно, думаете, что это делает. Я полагаю, вы имеете в виду 'if '>>>' не в строке, а 'Mooneye Studios' не в строке' –