简体   繁体   English

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

[英]Reading Specific Python list from a text file

I am currently Trying to read all data Occurring after a colon ':' in a file for example. 我目前正在尝试读取文件中冒号':'之后发生的所有数据。 In a text file containing only: 在仅包含以下内容的文本文件中:

SAM Account Type : 805306368 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)

This works like a charm only showing me "805306368" But when I try to check for more variable other than "SAM Account Type" by using a list it fails to give the correct output. 这就像一个魅力,仅向我显示“ 805306368”,但是当我尝试通过使用列表来检查“ SAM帐户类型”以外的更多变量时,它无法给出正确的输出。

For example the file below: 例如下面的文件:

SAM Account Name : Ramachandran. SAM帐户名:Ramachandran。 S 小号

SAM Account Type : 805306368 SAM帐户类型:805306368

Description : 说明:

User Account Control : 544 用户帐户控制:544

When Created:09/21/2015 06:33:53 创建时间:09/21/2015 06:33:53

Lastlogontimestamp : 130966421275783509 Lastlogontimestamp:130966421275783509

When Changed : 01/07/2016 12:08:47 更改时:01/07/2016 12:08:47

Account Expires : 922337203685477580 帐户过期:922337203685477580

Last logoff : 00:00:00.0000000 上次注销:00:00:00.0000000

Last logon : 130971364125825724 上次登录时间:130971364125825724

and this following code : 和下面的代码:

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=""

Which Reads all the Characters after a ':' which Belong to an Entity in the list 'A' Stores them in 'B', and prints B for each line. 它将读取“:”之后的所有字符,这些字符属于列表“ A”中的实体,将它们存储在“ B”中,并为每行打印B。

Gives The following: 给出以下内容:

'805306368' '805306368'
'544' '544'
'130966421275783509' '130966421275783509'
'922337203685477580' '922337203685477580'
'130971364125825724' '130971364125825724'

When it should also give for 'Last logoff' which is '00:00:00.0000000' But it doesn't work. 什么时候还应该给出“上次注销”,即“ 00:00:00.0000000”,但它不起作用。 Any help would be highly appreciated. 任何帮助将不胜感激。

I think you can read all lines and process them as per your requirement. 我认为您可以阅读所有行并根据需要进行处理。 You can split a sentence based on ":" and use tokens. 您可以根据“:”分割句子并使用标记。

Note: As time also has : in it, you may want to use " : " (colon with 2 spaces) 注意:由于时间中也包含:,因此您可能需要使用“:”(冒号2个空格)

sample code: 样例代码:

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

Further, 进一步,

  1. You can convert this to dict using the key and value pair you got from processing. 您可以使用从处理中获得的键和值对将其转换为字典。 Later you can dump this dict to json for other tasks. 稍后,您可以将此字典转储到json以执行其他任务。
  2. It seems like you are coming to python from C like language. 似乎您是从C语言开始使用python的。 In python, most of the things are inbuilt like reading a file, splitting a string, etc. So, refer to some tutorials like https://developers.google.com/edu/python/ , etc. to learn more 在python中,大多数事情都是内置的,例如读取文件,分割字符串等。因此,请参考一些教程,例如https://developers.google.com/edu/python/等,以了解更多信息。

As you are scanning for specific strings (ie those in A) I would create a list of each line in your file. 当您扫描特定的字符串(即A中的字符串)时,我将创建文件中每一行的列表。

Split each line by ' : ' which seems to be the standard break between your key and your values in your txt file. ' : '分隔每一行,这似乎是您的密钥和txt文件中的值之间的标准分隔符。

You now have a list that you can scan B and compare the first element of this list to the contents of A . 现在,您有了一个列表,可以扫描B并将该列表的第一个元素与A的内容进行A We can print the second element (what appears after ' : ' for each match: 我们可以打印第二个元素(每个匹配项在' : '之后出现的内容:

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

Another 'fun' way to do this would be to create a dictionary 另一种“有趣”的方式是创建字典

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]

If you're into the whole brevity thing: 如果您热衷于简洁:

for i in open("Sample.txt").readlines():
  if i[:i.index(' : ')] in A:
    print i[i.index(' : ')+3:].split()[0]

Lastly: 最后:

print ''.join([i[i.index(' : ')+3:] for i in open("Sample.txt").readlines() if i[:i.index(' : ')] in A])

If all you're trying to do is print values to the right of the first colon if they have a string in A, then you could: 如果您只想在第一个冒号的右边打印值(如果它们在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