使用正則表達式我正在通過包含數據的文本文件進行搜索。我得到類似這樣的輸出。 這是我得到例如:Python將多行寫入數組
36
37
36
36
36
76
39
36
68
36
56
36
36
36
...
我需要所有這些36是在陣列像這樣[「36」,「36」,...]示例代碼的下方。
#!/usr/bin/python
import re
log = re.compile('Deleted file number: first: (\d+), second (\d+), third (\d+), fourth (\d+), bw (\d+), value: ([\dabcdefx]+), secondvalue: ([\d.]+)LM, hfs: ([\d.-]+)ls')
logfile = open("log.txt", "r").readlines()
List = []
for line in logfile:
m = log.match(line)
if m:
first = int (m.group(1))
second = int (m.group(2))
third = int (m.group(3))
fourth = int (m.group(4))
bw = int (m.group(5))
value = int (m.group(6),0)
secondvalue = float (m.group(7))
hfs = float (m.group(8))
List.append(str(first)+","+str(second)+"," \
+str(third)+","+str(fourth)+"," \
+str(bw)+","+str(value)+"," \
+str(secondvalue)+","+str(hfs))
for result in List:
print(result)
我可以使用sys.stdout.write函數()來在一個單一的線路相同的帶有打印內容的項目, 顯示它可是我怎樣才能把所有這一切成一個陣列,以像陣列= [「149」, 149" , 「153」, 「153」 等]
任何幫助,將不勝感激
從驗證碼的相關摘錄會幫助我們。您可以創建一個列表併爲其添加每個值。 –
我得不到字符串,但是列 – user993298
當你將它們作爲字符串保存到數組中時,爲什麼要將它們轉換爲int和float。一個數組可以簡單[第一,第二,第三,...] – spicavigo