看板 Python 關於我們 聯絡資訊
test.txt 檔案內容如下 -------------- 1-10,4:6 其中1-10,4:6也有可能1-10 或 1-10,4 或 1-10,4:6:8 等等的情況產生 我希望可以由這個test.txt控制檔,控制我迴圈的執行 然後我寫了下面的方法,但是在上述的情況產生下(1-10 或 1-10,4 或 1-10,4:6:8 等) 我就不知道該怎麼下手了 , :都是我自行設定的 我只想要 1-10 迴圈1跑到10 1-10,4:5 迴圈1跑到10 ,遇到4.5不跑,6接下去跑 1-10,4 迴圈1跑到10 ,遇到4不跑,5接下去跑 id='' id_str='' id_end='' tmp='' f = open('test.txt') for i,str in enumerate(f, 1): if i == 2: id=str.strip() id_str=id.split('-')[0] id_end=id.split('-')[1].split(',')[0] #print(id) #print(id_str) #印出1 #print(id_end) #印出10 f.close() for i in range(int(id_str),int(id_end)+1): if i==4: #模擬類似這樣的效果 continue print (i) 不好意思,再次麻煩各位了,謝謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 42.71.25.164
uranusjr:看起來不錯啊, id.split('-')[2].split(':') 就有後面了 01/09 21:07
uranusjr:都快寫好了不懂你的問題在哪 01/09 21:07
我的問題是想要模擬,遇到4:6 在迴圈就會遇到4和6就跳出
ck574b027:逗號後面是什麼,不像step。 01/10 11:58
是我自己設定的分號;
ck574b027:用len檢查split(','),有東西的話拿來split(':'),done. 01/10 12:06
我會再試看看,看看要怎麼把它抓出來,放到迴圈中
ck574b027:話說行號二的i應該為1不是嗎? 01/10 12:08
1-10嗎? 是1沒錯 ※ 編輯: blackspace98 來自: 42.75.129.11 (01/10 23:38)