Sam
Sam

Reputation: 13

Replacing strings and RegEx in a file

This is my first question and I'm pretty new to Python.

Though, I really tried a few different things before asking :-D

I have a file (test.txt) containing some text organized as follow:

name="NAME-ABC"                                                         NewName="GOOD MORNING"
name="NAME-123456"                                                      NewName="HELLO"
name="NAME-3"                                                           NewName="ALLO"

The output I'm trying to obtain is a dictionary with the previous values, such as:

{"NAME-ABC":"GOOD MORNING","NAME-123456":"HELLO","NAME-3":"ALLO"}

Using a small dictionary ("d" in my example below), I managed to remove the first name= part and replace the endline by a comma, but the middle part (containing a non consistent number of spaces and tabs + NewName=), I cannot deal with it.

My code so far is :

d =    {
  "name=": "",
  "\n": ","}


for x in range(len(d)):

    with open("test.txt") as f:
        newText=f.read().replace(list(d.items())[x][0], list(d.items())[x][1])

    with open("test.txt", "w") as f:
        f.write(newText)

which gives me a text.txt file as follow:

"NAME-ABC"                                                      NewName="GOOD MORNING",
"NAME-123456"                                               NewName="HELLO",
"NAME-3"                                                        NewName="ALLO"

but if I put something like

d =    {
      "name=": "",
      "\n": ",",
       re.compile(r'\s*NewName='): ":"}

I get the following error

TypeError: replace() argument 1 must be str, not re.Pattern

I then tried to use re.sub to handle regex, but now, it just doesn't do anything at all (program runs, but no modification in the file):

for line in "test.txt":
    line = re.sub(r'\s*NewName=',":","test.txt")

Thanks in advance for your help folks

Upvotes: 1

Views: 89

Answers (1)

Rakesh
Rakesh

Reputation: 82785

Using Regex.

Ex:

import re

res = {}
with open(filename) as infile:
    for line in infile:                                   #Iterate Each line
        k, v = re.findall(r'=\"(.*?)\"', line.strip())    #Get Key-Value --> Get string in Quotes
        res[k] = v                                        #Form output
print(res)

Upvotes: 1

Related Questions