Reputation: 581
I need to convert or fetch values of integers at the end of the string from following 4 string from a text file .
Integer values at the last of each string constantly change, which is a problem here. How can i implement it through regex?
string1 = ('Port A Rx frames:\t', '1003')
string2 = ('Port B Rx frames:\t', '1000')
string3 = ('Tx FrameCount :', 'Port A', '1001')
string4 = ('Tx FrameCount :', 'Port B', '1001')
I tried regular expression but it is not working:
import os
import re
f1 = open("C:/path/Desktop/set_initial.txt")
s1 = "('Port A Rx frames:\t', '1003')"
s2 = "('Port B Rx frames:\t', '1000')"
s3 = "('Tx FrameCount A :', 'txstreamresults', '1001')"
s4 = "('Tx FrameCount B :', 'txstreamresults', '1001')"
for line in f1:
a = int(re.search(r'\d+', s1).group())
b = int(re.search(r'\d+', s2).group())
c = int(re.search(r'\d+', s3).group())
d = int(re.search(r'\d+', s4).group())
print(a)
print(b)
print(c)
print(d)
expected output :
1003
1000
1001
1001
observed output but i have kept above 4 strings as static however integer values at last of each string will dynamically change:
1003
1000
1001
1001
Upvotes: 0
Views: 272
Reputation: 7353
You could use re.findall('\d{4}', s)
to get a list of 4-digit strings in s
. The list could then be just joined or use index to get the first (and assuming the only result) since there is only one 4-digit number in each string.
import re
s1 = "('Port A Rx frames:\t', '1003')"
s2 = "('Port B Rx frames:\t', '1000')"
s3 = "('Tx FrameCount A :', 'txstreamresults', '1001')"
s4 = "('Tx FrameCount B :', 'txstreamresults', '1001')"
ss = [s1, s2, s3, s4]
[''.join(re.findall('\d{4}', s)) for s in ss]
['1003', '1000', '1001', '1001']
You join all strings together (assuming you could read the entire file as a single string)
re.findall('\d{4}', '\n'.join(ss))
['1003', '1000', '1001', '1001']
Upvotes: 1
Reputation: 518
Taking that your variables like s1
, s2
etc are taken directly from a text file, and it appears exactly as you have presented these variable's values in your code, eg:
s2 = "('Port B Rx frames:\t', '1000')"
s3 = "('Tx FrameCount A :', 'txstreamresults', '1001')"
s4 = "('Tx FrameCount B :', 'txstreamresults', '1001')"
You can use the following to get the 4 digit integer:
all_numbers = []
with open('number.txt', 'r') as opened_file:
for line in opened_file:
number = ''
for e in line.split(',')[-1]:
if e.isdigit():
number = number+e
value = int(number)
all_numbers.append(value)
all_numbers
The variable value
has the integer digit like 1001, 1000 etc. If you want to get the string form, ignore the conversion at value = int(number)
.
Upvotes: 1
Reputation: 1083
I hope you catch my idea about your issue.
import re
fname = "/path/to/set_initial.txt"
check_update = True
while check_update:
readfile = open(fname, 'r').read() # " a, 123 \n b, 234 \n c, 345"
list_of_numbers = re.findall(r'\d+', readfile) # ['123', '234', '345']
if updating_file: # traffic generator script run <== MOST IMPORTANT
check_update = True
else:
check_update = False
Upvotes: 1
Reputation: 82765
Looks like you just need.
import re
with open("C:/path/Desktop/set_initial.txt") as infile:
for line in infile:
if line.strip():
a = re.search(r'(\d+)', line)
if a:
print(int(a.group(1)))
Upvotes: 1