简体   繁体   English

从 txt 文件中提取特定数字并将其插入数据框中

[英]Extract specific numbers from txt files and insert them into a data frame

Good evening, I would need some help extracting two numbers from a text file and inserting them into a dataframe.晚上好,我需要一些帮助,从文本文件中提取两个数字并将它们插入 dataframe。

This is my txt file:这是我的 txt 文件:

PING 10.0.12.100 (10.0.12.100) 56(84) bytes of data.
64 bytes from 10.0.12.100: icmp_seq=1 ttl=59 time=0.094 ms
64 bytes from 10.0.12.100: icmp_seq=2 ttl=59 time=0.070 ms
64 bytes from 10.0.12.100: icmp_seq=3 ttl=59 time=0.076 ms
64 bytes from 10.0.12.100: icmp_seq=4 ttl=59 time=0.075 ms
64 bytes from 10.0.12.100: icmp_seq=5 ttl=59 time=0.070 ms
64 bytes from 10.0.12.100: icmp_seq=6 ttl=59 time=0.060 ms
64 bytes from 10.0.12.100: icmp_seq=7 ttl=59 time=0.093 ms
64 bytes from 10.0.12.100: icmp_seq=8 ttl=59 time=0.080 ms
64 bytes from 10.0.12.100: icmp_seq=9 ttl=59 time=0.077 ms
64 bytes from 10.0.12.100: icmp_seq=10 ttl=59 time=0.082 ms
64 bytes from 10.0.12.100: icmp_seq=11 ttl=59 time=0.070 ms
64 bytes from 10.0.12.100: icmp_seq=12 ttl=59 time=0.075 ms
64 bytes from 10.0.12.100: icmp_seq=13 ttl=59 time=0.087 ms
64 bytes from 10.0.12.100: icmp_seq=14 ttl=59 time=0.069 ms
64 bytes from 10.0.12.100: icmp_seq=15 ttl=59 time=0.072 ms
64 bytes from 10.0.12.100: icmp_seq=16 ttl=59 time=0.079 ms
64 bytes from 10.0.12.100: icmp_seq=17 ttl=59 time=0.096 ms
64 bytes from 10.0.12.100: icmp_seq=18 ttl=59 time=0.071 ms

--- 10.0.12.100 ping statistics ---
18 packets transmitted, 18 received, 0% packet loss, time 17429ms
rtt min/avg/max/mdev = 0.060/0.077/0.096/0.013 ms

I would like to have a dataframe like this:我想要一个像这样的 dataframe:

在此处输入图像描述

This is my code:这是我的代码:

import pandas as pd

df = pd.DataFrame(columns=["ICMP_SEQ", "TIME"])

with open("/content/H11H22_ping.txt", "r") as f:
  txt = f.read() 
  print(txt)

  // code

  df = df.append({"ICMP_SEQ": icmp_seq, "TIME": time})

Thanks谢谢

Use str.extract :使用str.extract

df = pd.read_csv('/content/H11H22_ping.txt', skiprows=1, header=None, names=['logs'])
res = df['logs'].str.extract(r'icmp_seq=(?P<icmp_seq>\d+)\b.+\btime=(?P<time>\d+\.\d+)', expand=True)
print(res)

Output (partial) Output (部分)

   icmp_seq   time
0         1  0.094
1         2  0.070
2         3  0.076
3         4  0.075
4         5  0.070
5         6  0.060
6         7  0.093
7         8  0.080
8         9  0.077
9        10  0.082
10       11  0.070
11       12  0.075
12       13  0.087
13       14  0.069
14       15  0.072
15       16  0.079
16       17  0.096
17       18  0.071
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要使用Python从多个.txt文件中提取数据并将其移至Excel文件 - I need to extract data from multiple .txt files and move them to an Excel file, using Python 从多个数据文件中提取列并将它们保存到新数据框中的单独列中 - Extract columns from multiple data files and save them into separate columns in a new data frame 从 python 中的 txt 文件中提取特定数据 - extract specific data from txt file in python 从 Txt 文件中提取特定数据 python - Extract Specific Data from Txt file python 如何从数据框列中提取特定项目并将其用作其余项目的标签? - How to extract specific items from a data frame column and use them as labels for the remaining items? 使用 glob 导入多个 excel 文件并迭代处理它们并从结果中提取数据框 - Import several excel files using glob and iteratively work on them and extract a data frame from results 我想从 TXT 文件中提取包含 x 和 y 数据的特定行,并按 y 数据对它们进行排序 - I want to extract specific lines which contains x & y data from a TXT file and sort them by y data 如何从 msg 文件中提取数据并将它们插入(附加)到 csv 文件中? - How to extract data from msg files and insert (append) them to csv file? 如何从两个不同的txt文件中对数字进行排序然后将它们另存为一个txt文件 - How to sort numbers from two different txt files then save them as one txt file 从.txt提取数据 - Extract data from .txt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM