提取特定部分這是我的Python文件如何從文本文件中python3
path = '/my/file/list.txt'
with open(path,'rt') as file:
print("step 1")
collected_lines = []
started = False
for line in file:
for n in range(1, 10):
if line.startswith('PLAY NO.{}'.format(n)):
started = True
print("started at line {}".format(line[0]))
continue
if started:
collected_lines.append(line)
if started and line == 'PLAY NO.{}'.format(n+1):
print("end at line {}".format(line[0]))
break
print(collected_lines.append(line))
這是我的代碼.. OUTPUT:
None
None
None
None
None
None
現在我想從行開始玩No2玩No3 ....但我得到無..請任何建議...我正在使用Python 3.5
對不起,這是第一次問這個網站的問題.. 我的文件看起來像這樣..
TextFile.txt的
Hello and Welcome This is the list of plays being performed here
PLAY NO. 1
1. adknjkd
2. skdi
3. ljdij
PLAY NO. 2
1. hsnfhkjdnckj
2. sjndkjhnd and so on
什麼線?你不顯示行。 –
我們需要一個文件示例 –
我會推薦過濾你想要的行。看看http:// stackoverflow。com/questions/2401785/in-python-can-i-single-line-a-for-loop-over-iterator-with-an-if-filter可能會給你提示如何以一種方便的方式做到這一點。此外,我會建議使用正則表達式來過濾像'None!= re.match(「^ No [0-9] [^ 0-9]」,line)' – TobiSH