数据

slot : acu:1/1                         plannedtype : ngfcf                       actualtype : ngfcf                           operstatus : enabled
errorstatus : noerror                                                                       availability : available
alarmprofile : none                                                                          capabprofile : not_applicable

slot : acu:1/2                         plannedtype : ngfcf                       actualtype : ngfcf                           operstatus : enabled
errorstatus : noerror                                                                       availability : available
alarmprofile : none                                                                          capabprofile : not_applicable

slot : acu:1/3                       plannedtype : ngfcf                       actualtype : ngfcf                           operstatus : enabled
errorstatus : noerror                                                                       availability : available
alarmprofile : none                                                                          capabprofile : not_applicable




  正如我们看到的槽和其他项被重复一样,我的循环仅将要迭代的csv文件中插入第一个项目,并在“:”之后将这些值添加到csv文件的下一行。



#reading a text file

import re
from collections import defaultdict
import pandas as pd

result = defaultdict(list)

with open('gthamelslot.txt') as f:
    for line in f:
        match = re.findall(r"\s+(\S+)\s+:\s+(\S+)", line)
        for k,v in match:
            if k in ["slot",
                     "errorstatus",
                     "alarmprofile",
                     "manufacturer",
                     "mnemonic",
                     "pbacode",
                     "fpbacode",
                     "fpbaics",
                     "cleicode",
                     "serialno",
                     "failedtest",
                     "ltrestarttime",
                     "plannedtype",
                     "ltrestartcause"]:
                result[k].append(v)

df = pd.DataFrame(result)
writetocsv = df.to_csv("test.csv")
print(df)






  我得到的输出


slot     plannedtype  actualtype  errorstatus
acu:1/1  ngfcf         ngfcf        noerror



  需要输出


slot     plannedtype  actualtype  errorstatus
acu:1/1  ngfcf         ngfcf        noerror
acu:1/2  ngfcf         ngfcf        noerror
acu:1/3  ngfcf         ngfcf        noerror

最佳答案

以下更改:

将正则表达式更改为r“ \ b(\ S +)\ s +:\ s +(\ S +)”以允许在行首进行匹配(更改以查找单词边界)

当比较列表中的某些项目(例如“ planned-type”)中的文件名不是“-”时(在您想要的字段中删除“-”,例如plannedtype,actualtype,errorstatus)。

import re
from collections import defaultdict
import pandas as pd

result = defaultdict(list)

with open('gthamelslot.txt') as f:
    for line in f:
        match = re.findall(r"\b(\S+)\s+:\s+(\S+)", line)
        for k,v in match:
            if k in ["slot",
              "actualtype",
              "errorstatus",
              "alarm-profile",
              "manufacturer",
              "mnemonic",
              "pbacode",
              "fpbacode",
              "fpbaics",
              "cleicode",
              "serialno",
              "failedtest",
              "ltrestarttime",
              "plannedtype",
              "ltrestartcause"]:
                result[k].append(v)

df = pd.DataFrame(result)
writetocsv = df.to_csv("test.csv")
print(df)


输出量

slot plannedtype actualtype errorstatus
0  acu:1/1       ngfcf      ngfcf     noerror
1  acu:1/2       ngfcf      ngfcf     noerror
2  acu:1/3       ngfcf      ngfcf     noerror

关于python - 我的代码只循环到某个点,然后忽略该Python,字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59479249/

10-14 19:38
查看更多