Parsing text file in python
<p>So I am trying to python program that will extract the round trip time from a web server ping stored in a text file. So what I basically have is a text file with this:</p>

<pre><code>    PING e11699.b.akamaiedge.net (104.100.153.112) 56(84) bytes of data.
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=17.2ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=12.6ms
    64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=11.7ms
    ... (a bunch more ping responses here)
    --- e11699.b.akamaiedge.net ping statistics ---
    86400 packets transmitted, 86377 received, 0% packet loss, time 86532481ms
    rtt min/avg/max/mdev = 6.281/18.045/1854.971/28.152 ms, pipe 2
</code></pre>

<p>I am very new to python and need help being able to use regex commands to extract only the times between "time=" and "ms"and send it to another text file to look like:</p>

<pre><code>11.7
12.6
17.2
...
</code></pre>

<p>Any help would be greatly appreciated!</p>

<p>You specified that your data is already in a text file. So asuming that your text file is called <code>data.txt</code></p>

<pre><code>#we will be using the regular expression library for this example
import re 

#open the "data.txt" (named data_file in a scope)
with open("data.txt") as data_file: 
    #read the text from the data_file into ping_data
    ping_data = data_file.read() 
    found_data = re.findall('time=(.*)ms', ping_data)

with open('found.txt', 'w') as found_file:
    for pattern in found_data:
        found_file.write(pattern+"\n")
</code></pre>

<p>This fill output a file called <code>found.txt</code> with the following:</p>

<pre><code>17.2
12.6
11.7
</code></pre>

<p>In the example we just open your data.txt file. Then read the data form it. Then find all the occurrences of the regular expression pattern that will return the data you are looking for.</p>

<p><code>time=(.*)ms</code> means *a string of any size between the letters <code>time=</code> and <code>ms</code></p>

<p>Then after we have found the patern we simply write it to another file called <code>found.txt</code>, writing one line at a time until its complete.</p>

text-processing python