[英]Reading Specific Python list from a text file
我目前正在嘗試讀取文件中冒號':'之后發生的所有數據。 在僅包含以下內容的文本文件中:
SAM帳戶類型:805306368
with open("Sample.txt") as myfile:
for line in myfile:
flag=0
if ("SAM Account Type" in line):
for ch in line:
if (flag and ch!=' ' and ch!='\n'):
B+=ch
elif (ch == ':'):
flag+=1
S1 = myfile.read(10)
# print (ch)
elif (ch=='\n'):
flag =0
else:
pass
print (B)
這就像一個魅力,僅向我顯示“ 805306368”,但是當我嘗試通過使用列表來檢查“ SAM帳戶類型”以外的更多變量時,它無法給出正確的輸出。
例如下面的文件:
SAM帳戶名:Ramachandran。 小號
SAM帳戶類型:805306368
說明:
用戶帳戶控制:544
創建時間:09/21/2015 06:33:53
Lastlogontimestamp:130966421275783509
更改時:01/07/2016 12:08:47
帳戶過期:922337203685477580
上次注銷:00:00:00.0000000
上次登錄時間:130971364125825724
和下面的代碼:
A = []
A.extend({"SAM Account Type",
"User Account Control",
"Last logon",
"Lastlogontimestamp",
"Last logoff",
"Account Expires"})
B = ""
with open("Sample.txt") as myfile:
for line in myfile:
for i in range(len(A)):
flag=0
if (str(A[i]) in line):
#if ("SAM Account Type" in line):
for ch in line:
if (flag and ch!=' ' and ch!='\n'):
B+=ch
elif (ch == ':'):
flag+=1
S1 = myfile.read(10)
elif (ch=='\n'):
flag =0
else:
pass
print (B)
B=""
它將讀取“:”之后的所有字符,這些字符屬於列表“ A”中的實體,將它們存儲在“ B”中,並為每行打印B。
給出以下內容:
'805306368'
'544'
'130966421275783509'
'922337203685477580'
'130971364125825724'
什么時候還應該給出“上次注銷”,即“ 00:00:00.0000000”,但它不起作用。 任何幫助將不勝感激。
我認為您可以閱讀所有行並根據需要進行處理。 您可以根據“:”分割句子並使用標記。
注意:由於時間中也包含:,因此您可能需要使用“:”(冒號2個空格)
樣例代碼:
In [1]: with open("./input.txt") as f:
...: data = f.readlines()
...:
In [2]: data = [d for d in data if d!='\n'] #Drop empty lines
In [3]: data = [d[:-1].split(" : ") for d in data] # exclude \n (last char in the line) and split based on colon
In [4]: data
Out[4]:
[['SAM Account Name', 'Ramachandran. S'],
['SAM Account Type', '805306368'],
['Description :'],
['User Account Control', '544'],
['When Created:09/21/2015 06:33:53'],
['Lastlogontimestamp', '130966421275783509'],
['When Changed', '01/07/2016 12:08:47'],
['Account Expires', '922337203685477580'],
['Last logoff', '00:00:00.0000000'],
['Last logon', '130971364125825724']]
進一步,
當您掃描特定的字符串(即A中的字符串)時,我將創建文件中每一行的列表。
用' : '
分隔每一行,這似乎是您的密鑰和txt文件中的值之間的標准分隔符。
現在,您有了一個列表,可以掃描B
並將該列表的第一個元素與A
的內容進行A
。 我們可以打印第二個元素(每個匹配項在' : '
之后出現的內容:
B=[]
with open("Sample.txt") as f:
for line in f:
B.append(line.split(' : ')
for i in B:
if i[0] in A:
print i[1].strip() #this removes the \n
另一種“有趣”的方式是創建字典
c={}
with open("Sample.txt") as f:
for line in f:
t=line.split(' : ')
c.update({t[0]:t[1].split()})
for i in set(c.keys()) & set(A): #gives set of matches between keys and A
print c[1]
如果您熱衷於簡潔:
for i in open("Sample.txt").readlines():
if i[:i.index(' : ')] in A:
print i[i.index(' : ')+3:].split()[0]
最后:
print ''.join([i[i.index(' : ')+3:] for i in open("Sample.txt").readlines() if i[:i.index(' : ')] in A])
如果您只想在第一個冒號的右邊打印值(如果它們在A中有一個字符串),那么您可以:
for line in myfile:
split_line = line.split(' : ', 1) #splits into list of two elements at first colon unless colon is not found, then a list of one element
if split_line[0] in A and len(split_line)==2:
print split_line[1].replace('\n', '')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.