-3

I am displaying data from a file by using a regular expression. I want to use:

ID=02141592cc0000000700000000000000 
ID=02141592cc000000010000000e9489c5 
ID=02141592cc0000000500000000000000 
ID=02141592cc000000010000000e9489c5
ID=02141592cc0000000400000000000000 
ID=02141592cc000000010000000e9489c5

I am using this funtion to extract data:

data = OrderedDict({

    'data': r'\b\s\sID=(\w{32})0*',
   })

def Extract_List_Data_Info_Based_On_Regular_Expression(Log_File):
    # print(re_key)
    for re_key in data.keys():

        print(re_key)
        re_value = data[re_key]
        fsrc = open(Log_File, 'r')
        buff = fsrc.read()
        list_info = re.findall(re_value, buff)
        print(list_info)

My goal is to extract data, then modify the last 14 numbers with 0. I want write the modification in the file. For example: this ID=02141592cc000000010000000e9489c5 then I want to modify it it to ID=02141592cc0000000100000000000000

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Sony
  • 5
  • 2
  • @Austin Thank you I edit it last 14. – Sony Mar 01 '19 at 10:05
  • @JackMoody could you give me more exlpanation? It is not clear – Sony Mar 01 '19 at 10:06
  • my answer wasn’t exactly right. You probably want to use `my_string = my_string[:-14] + “0”*14`. This will take your original string, cut off the last 14 characters and then add 14 zeros. – Jack Moody Mar 01 '19 at 10:10
  • 1
    Possible duplicate of [Remove final character from string (Python)](https://stackoverflow.com/questions/15478127/remove-final-character-from-string-python) – Jack Moody Mar 01 '19 at 10:15

2 Answers2

3

This will work for you

s1 = "ID=02141592cc000000010000000e9489c5"
print s1[:-14] + '0'*14
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
0

I will write another answer, similar to Amit with repace()

ID = ID.replace(ID[len(ID)-14:], "0"*14)

That's the same idea behind. You don't need regular expression to do what you wanna do :)

Maxouille
  • 2,729
  • 2
  • 19
  • 42