Insert replace text based on a name in other file python script

Joined
Mar 5, 2025
Messages
2
Reaction score
0
I have two text files, file-1 contains a list of 38476 names with a path separated by a space sometimes the name is the same as the path name and can contain numbers.

What my script needs to do is read file-1 for every matching name insert a path into a line in file-2 the blocks of data I need to change on specific lines always have StaticMesh=StaticMesh'REPLACEWORD'. Can somebody help me modify my python script

example
Code:
StaticMesh=StaticMesh'Dog.Dog'

Becomes
Code:
StaticMesh=StaticMesh'/Game/Meshes/Bulldog.Bulldog'

(file-1.txt) I can edit this file to be Bulldog.Bulldog etc the double names are necessary.
Code:
Dog /Game/Meshes/Bulldog
Fish /Game/Meshes/Goldfish
Cat /Game/Meshes/Cat

(file-2.txt)
Code:
Begin Map
Begin Level
 
Begin Map
   Begin Level
      Begin Actor Class=StaticMeshActor Name=Dog Archetype=StaticMeshActor'/Script/Engine.Default__StaticMeshActor'
         Begin Object Class=StaticMeshComponent Name=StaticMeshComponent0 ObjName=StaticMeshComponent0 Archetype=StaticMeshComponent'/Script/Engine.Default__StaticMeshActor:StaticMeshComponent0'
         End Object
         Begin Object Name=StaticMeshComponent0
            StaticMesh=StaticMesh'Dog.Dog'
            RelativeLocation=(X=-80703.9,Y=-91867.0,Z=7863.95)
            RelativeScale3D=(X=1.0,Y=1.0,Z=1.0)
            RelativeRotation=(Pitch=0.0,Yaw=-169.023,Roll=0.0)
         End Object
         StaticMeshComponent='StaticMeshComponent0'
         RootComponent='StaticMeshComponent0'
         ActorLabel="Dog"
      End Actor
   End Level
Begin Surface
End Surface
End Map

Python:
import re


def main():
    file_one = 'file-1.txt'
    file_two = 'file-2.txt'

    word_tuples = []

    with open(file_one, 'r') as file:
        for line in file:
            words = line.split()  # splits on white space
            word_tuples.append((words[0], words[1]))

    new_lines = []
    with open(file_two, 'r') as file:
        for line in file:
            # Look for StaticMesh lines
            match = re.search(r"StaticMesh=?'(.+)'", line)
            if not match:
                new_lines.append(line.strip())
                continue

            quoted_word = match.group(1)

            # see if any of the lines from file 1 match it
            was_found = False
            for word_tuple in word_tuples:
                if word_tuple[0] == quoted_word:
                    new_lines.append(line.replace(quoted_word, word_tuple[1]))
                    was_found = True
            if not was_found:
                new_lines.append(line.strip())

    for new_line in new_lines:
        print(new_line)


if __name__ == '__main__':
    main()
 
Joined
Mar 5, 2025
Messages
2
Reaction score
0
This code is working but it won't replace a line if there is a double word Dog.Dog for instance how can I change it any idea?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,120
Latest member
Natbelix

Latest Threads

Top