繁体   English   中英

从文本文件读取特定的Python列表

[英]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']]

进一步,

  1. 您可以使用从处理中获得的键和值对将其转换为字典。 稍后,您可以将此字典转储到json以执行其他任务。
  2. 似乎您是从C语言开始使用python的。 在python中,大多数事情都是内置的,例如读取文件,分割字符串等。因此,请参考一些教程,例如https://developers.google.com/edu/python/等,以了解更多信息。

当您扫描特定的字符串(即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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM