[英]Question about nested loops
我是編程新手,在解決嵌套循環時遇到一些問題。 我有一個要從較大文件中提取的數據列表。 我能夠從較大的文件中成功提取一項數據,但是我需要從數千個試驗的較大文件中提取100種不同的試驗。 每個試用版都是較大文件的一行數據。 這是我用來一次成功提取一行數據的程序。 在此示例中,它提取了試驗1的數據。它基於我在先前的問題和教程中看到的示例。 問題是我不需要1-100次試用,也不需要任何有序模式。 我需要試用134、274、388等。它會跳過。 因此,如果它沒有我可以輸入的范圍,我不知道如何使用for語句進行嵌套循環。 任何幫助表示贊賞。 謝謝。
completedataset = open('completedataset.txt', 'r')
smallerdataset = open('smallerdataset.txt', 'w')
for line in completedataset:
if 'trial1' in line: smallerdataset(line)
completedataset.close()
smallerdataset.close()
我真的很想這樣:
試驗=('trial12','trial23','trial34')
用於completedataset中的行:用於試驗中的試驗:如果用於行中的試驗:smalldataset(line)
但這不起作用。 誰能幫助我修改此程序,使其正常運行?
在我看來,您需要一個列表,其中包含您感興趣的所有試用編號。因此,也許您可以嘗試執行以下操作:
completedataset = open('completedataset.txt', 'r')
smallerdataset = open('smallerdataset.txt', 'w')
trials = [134, 274, 388]
completedata = completedataset.readlines()
for t in trials:
for line in completedata:
if "trial"+str(t) in line:
smallerdataset.write(line)
completedataset.close()
smallerdataset.close()
您可以這樣做:
trials = ['trial1', 'trial134', 'trial274']
for line in completedataset:
for trial in trials:
if trial in line: smallerdataset(line)
為了提高操作效率,您可以將每行與'trial [0-9] +'-regex匹配,並查看是否可以從集中找到該符號。
如果成套測試中的每個試驗都是已知的字節大小,則可以使用file.seek(n)
,其中n
是開始讀取的字節。 例如,如果文件中的每一行長為3個字節,則可以執行以下操作:
myfile = open('file.txt', 'r')
myfile.seek(lineToStartAt * 3)
myfile.readline()#etc
如果每行的字節數是可變的或未知的,則只需讀入行並丟棄不需要的行即可(如KLee1的回答 )
假設您提前知道試驗的內容,則可以
trials = ('trial12', 'trial23', 'trial34')
for line in completedataset:
for trial in trials:
if trial in line: smallerdataset(line)
您在指定試驗方式時會遇到一些問題。 如果您查找包含“ trial1”的行,那么您還將獲得包含“ trial123”的行。 如果較大的數據集以某種方式構造,則可以嘗試在特定字段中查找試用編號。 例如,如果數據是逗號分隔的,則可以使用csv包。 最后,使用生成器表達式而不是循環將使事情更簡潔。 假設試驗編號在數據集的第一欄中,您可以執行以下操作:
import csv
trials = ['trial134', 'trial1', 'trial56']
data = csv.reader(open('completedataset.txt'))
with open('smalldataset.txt','w') as outf:
csv.writer(outf).writerows(l for l in data if l[0] in trials)
假設您有一個函數,看着一行,就可以告訴您該行是否“符合要求”,那么代碼的正確結構將非常簡單:
with open('completedataset.txt', 'r') as completedataset:
with open('smallerdataset.txt', 'w') as smallerdataset:
for line in completedataset:
if iwantthisone(line):
smallerdataset.write(line)
with
語句將為您完成結賬。 在Python 2.7中,您可以將with
s的兩個合並為一個; 在Python 2.5中,您需要使用from __future__ import with_statement
來啟動模塊; 在目前最廣泛使用的Python 2.6版本中,以上代碼是正確的形式。
因此,絕對一切都歸結為該iwantthisone
函數。 您沒有告訴我們有關行格式的任何信息,這使我們無法進一步為您提供幫助。 但是,例如,假設每行中的第一個單詞標識測試,例如test432 ...
,並且您在名為want_these
的集合中擁有想要的測試want_these
,例如set([113, 432, 251, ...])
want_these
set([113, 432, 251, ...])
。 然后,編寫iwantthisone
一種非常簡單的方法可能是:
def iwantthisone(line):
firstword = line.split(None, 1)[0]
testnumber = int(firstword[4:])
return testnumber in want_these
的正確內容iwantthisone
完全取決於你的線條的格式,你如何知道你其實想保持當然,什么線路。 但我希望這種總體結構仍然有幫助。
請注意,我建議的這種常規結構中實際上沒有嵌套循環!-)
關於您在注釋中顯示的錯誤消息:行繼續符是一個反斜杠,因此它告訴您在該行中某個地方有一個錯誤的反斜杠字符。
假設行始終以試驗標識符開頭,則可以使用startswith函數和過濾器來提取所需的標識符。
completedataset = open('completedataset.txt', 'r')
smallerdataset = open('smallerdataset.txt', 'w')
wantedtrials = ('trial134', 'trial274', 'trial388')
for line in completedataset:
if filter(line.startswith, wantedtrials):
smallerdataset.write(line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.