【问题标题】:search specific set of words in .txt file在 .txt 文件中搜索特定的单词集
【发布时间】:2021-03-17 17:09:31
【问题描述】:

我有一个 txt 文件,其中包含以下数据集作为列表

Name:AP_A
Ch:0
Ptx:20
CCA:-68
AvgThroughput:{}
Data packets_sent:{}
Data_packets lost:{}
rts_cts_sent:{}
rts_cts_lost:{}
in-degA:0.0006766529737718963
out-degA:1.1814245426625214
-----------------
Name:AP_B
Ch:0
Ptx:5
CCA:-90
AvgThroughput:{}
Data packets_sent:{}
Data_packets lost:{}
rts_cts_sent:{}
rts_cts_lost:{}
in-degB:1.6025829114087657
out-degB:0.0006766529737718963

我需要搜索这些行/单词并将它们作为下一个数据集

---AP_A data---
Name:AP_A
in-degA:0.0006766529737718963
out-degA:1.1814245426625214
---AP_B data---
Name:AP_B
in-degB:1.6025829114087657
out-degB:0.0006766529737718963

我有一个代码来做这个,但我不能让我描述

archivo_ficha= "ficha_nodos_triang28.txt"
with open(archivo_ficha,'r') as inputfile:
     lines = []
     for line in inputfile:
         lines.append(line)

         search_words1=['Name:AP_A','in-degA','out-degA','Name:AP_B','in-degB','out-degB']
         for line in inputfile:
             if any(word in line  for word in search_words1):
                print("---datos_NodoA---")
                print(line)

                print("---datos_NodoB---")
                print(line)

提前致谢

【问题讨论】:

  • 您是否只想打印数据?您可以使用 grep:grep -E '^(Name|(in|out)-deg[AB]):' filename 做到这一点,并且几乎完全符合您的要求。
  • 嗯,是的,我需要把它放在控制台屏幕上,但也要把它作为一个变量,以便以后使用该数据,感谢您的贡献伙伴

标签: python string search readlines txt


【解决方案1】:

正如PaulProgrammer 建议的那样,您可以使用regular expressions。在 Python 中:

import re
archivo_ficha = "ficha_nodos_triang28.txt"
matches = [re.search(r"(Name|(in|out))(.+)", line) for line in open(archivo_ficha, 'r')]
matches = [m.group() for m in matches if m]

matches 是一个列表,您可以从中提取必要的数据:

['Name:AP_A',
 'in-degA:0.0006766529737718963',
 'out-degA:1.1814245426625214',
 'Name:AP_B',
 'in-degB:1.6025829114087657',
 'out-degB:0.0006766529737718963']

然后可以将它们分成 3 个一组并产生您想要的输出。

解释:

re.search 扫描字符串以寻找与该模式匹配的子字符串。 这里的模式是(Name|(in|out))(.+)

  • 第一部分Name|(in|out)表示:
    1. 找到Name
    2. 如果找不到,请查找inout
    3. 如果找到匹配项,则继续执行。否则,搜索将移至下一行。
  • 第二部分(.+)由特殊字符组成,以匹配字符串的其余部分:
    • . 匹配任何字符(换行符除外)
    • + 匹配前一个字符 (.) 1 次或多次

【讨论】:

  • 感谢或分享这位朋友,感谢您解决此问题
【解决方案2】:

你知道你有数据 A 和数据 B。你知道你从感兴趣的行中得到一个带有“AP_X”或“degX”的字符串。另外,你想打印一个标志来说明你的数据进入。

嗯,您的数据以“名称:AP_X”开头。

您将 A 和 B 的所有“写入”变量设置为 false。当您第一次遇到“名称:AP_A”时,打开 write_A,保持 write_B 关闭,打印不会打印两次的标题(仅当 write_A = False 和“名称:AP_A”在行中时才会导致),然后编写行包含感兴趣的标签。

archivo_ficha= "ficha_nodos_triang28.txt"

with open(archivo_ficha,'r') as inputfile:

     write_A = False; write_B = False; out_list = []

     for line in inputfile:

         if 'AP_A' in line and write_A == False:
            out_list.append("---datos_NodoA---"); print (out_list[-1])
            write_A = True; write_B = False

         if write_A == True and 'AP_A' in line or 'degA' in line:
            out_list.append(line.strip()); print (out_list[-1])


         if 'AP_B' in line and write_B == False:
            out_list.append("---datos_NodoA---"); print (out_list[-1])
            write_B = True; write_A = False

         if write_B == True and 'AP_B' in line or 'degB' in line:
             out_list.append(line.strip()); print (out_list[-1])

     inputfile.close()

输出:

---datos_NodoA---
Name:AP_A
in-degA:0.0006766529737718963
out-degA:1.1814245426625214
---datos_NodoB---
Name:AP_B
in-degB:1.6025829114087657
out-degB:0.0006766529737718963

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多