Reputation: 121
i am new to python and trying to write a simple code that reads lines from a given file, i am having a problem extracting a hexadecimal number from a line : for example i have the following file lines:
# Program start at address 0, after reset.
I@0x00000000
D@0x000032A0
i want to extract the two hexadecimal numbers (they represent the start of instruction and data addresses)
i wrote the following code and the method "get_start" is supposed to extract the first integer which is zero but instead i get the following output:
['0', '00000000']
my code :
import sys
import re
class Memory:
def __init__(self):
self.progStart=0
self.data_start=0
def get_start(self, line):
patterns = [r'\d+']
for p in patterns:
match = re.findall(p, line)
print(match)
return match
def sim_mem_reset(self, memFileName):
with open(memFileName, 'r+') as fileName:
for line in fileName:
if line[0] == '#' or len(line.strip()) == 0:
continue
if line[0] == 'I' and line[1] == '@':
self.progStart = self.get_start(line)
print(self.progStart)
if line[0] == 'D' and line[1] == '@':
# main
memory = Memory()
file_name = sys.argv[1]
print("Loading memory image file:", file_name)
memory.sim_mem_reset(file_name)
Upvotes: 0
Views: 45
Reputation: 44828
You don't need regexes. It's very simple to solve using str.strip
and the int
class:
def get_start(self, line):
'''
Assumes `line` looks like: I@0x01234567
'''
return int(line.strip()[2:], 16)
Upvotes: 3
Reputation: 5935
The number is hexadecimal, and you can extract it by using str.split()
:
s = 'I@0x00000000'
num = int(s.split('@')[1], 16)
This will extract the hexadecimal number after the '@' character.
Upvotes: 1