簡體   English   中英

Python初學者-如何將幾個文件的內容讀入唯一列表?

[英]python beginner - how to read contents of several files into unique lists?

我想將幾個文件中的內容讀入一個唯一的列表中,以便以后使用-最終,我想將這些列表轉換為集合並對其進行相交和相減。 這肯定是一個非常幼稚的問題,但是仔細研究了Lutz的“學習Python”的迭代器和循環部分之后,我似乎無法全神貫注於如何實現這一點。 這是我寫的:

#!/usr/bin/env python

import sys

OutFileName = 'test.txt'
OutFile = open(OutFileName, 'w')

FileList = sys.argv[1: ]
Len = len(FileList)
print Len

for i in range(Len):
    sys.stderr.write("Processing file %s\n" % (i))
    FileNum = i

for InFileName in FileList:
    InFile = open(InFileName, 'r')
    PathwayList = InFile.readlines()
    print PathwayList
    InFile.close()

通過幾個簡單的測試文件,我得到如下輸出:

處理文件0

處理文件1

['alg1 \\ n','alg2 \\ n','alg3 \\ n','alg4 \\ n','alg5 \\ n','alg6']

['csr1 \\ n','csr2 \\ n','csr3 \\ n','csr4 \\ n','csr5 \\ n','csr6 \\ n','csr7 \\ n','alg2 \\ n',' alg6']

這些列表是正確的,但是如何將每個列表分配給一個唯一變量,以便以后可以調用它們(例如,通過在變量名稱中包含來自range的索引號)?

非常感謝您為正確的方向指明了一個完整的編程初學者!

#!/usr/bin/env python

import sys

FileList = sys.argv[1: ]
PathwayList = []
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % (i))
    InFile = open(InFileName, 'r')
    PathwayList.append(InFile.readlines())
    InFile.close()

假設您讀了兩個文件,下面的代碼將逐行進行比較(它不會在較長的文件中占用任何多余的行,但是如果一個行比另一個行多,它們將是不同的;)

for i, s in enumerate(zip(PathwayList[0], PathwayList[1]), 1):
    if s[0] == s[1]:
        print i, 'match', s[0]
    else:
        print i, 'non-match', s[0], '!=', s[1]

對於您想做的事情,您可能想看一下Python中的difflib模塊。 為了進行排序,請查看Mutable Sequence TypessomeListVar.sort()將在適當位置對someListVar的內容進行排序。

如果不需要記住內容的來源,則可以這樣做:

PathwayList = []
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList.append(InFile.readlines())
    InFile.close()  

for contents in PathwayList:
    # do something with contents which is a list of strings
    print contents  

或者,如果您想跟蹤文件名,則可以使用字典:

PathwayList = {}
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList[InFile] = InFile.readlines()
    InFile.close()

for filename, contents in PathwayList.items():
    # do something with contents which is a list of strings
    print filename, contents  

您可能想檢查Python的fileinput模塊,它是標准庫的一部分,並允許您一次處理多個文件。

本質上,您具有文件列表,並且想要更改為這些文件的行列表...

幾種方法:

result = [ list(open(n)) for n in sys.argv[1:] ]

這將為您提供-> [[['alg1','alg2','alg3'],['csr1','csr2'...]]的結果,訪問將類似於'result [0]'在['alg1','alg2','alg3']中...

字典可能更好一些:

result = dict( (n, list(open(n))) for n in sys.argv[1:] )

如果只想串聯,則只需將其鏈接:

import itertools
result = list(itertools.chain.from_iterable(open(n) for n in sys.argv[1:]))
# -> ['alg1', 'alg2', 'alg3', 'csr1', 'csr2'...

對於初學者來說不是一線希望...但是現在,嘗試了解正在發生的事情將是一個不錯的選擇:)

您需要為正在讀取的每個文件“編號”動態創建變量名。 (我故意含糊不清,知道如何構建這樣的變量是非常有價值的,如果您自己發現它,更容易記住)

這樣會給你一個開始

您需要一個包含PathwayList列表的列表,即列表列表。

一句話:使用大寫的變量名是很不常見的。 對此沒有嚴格的規定,但是按照慣例,大多數人只對類使用大寫名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM