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

Joined
Mar 5, 2025
Messages
3
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
3
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?
 
Joined
Feb 3, 2025
Messages
10
Reaction score
1
# Read the name-replacement mapping from a file
def load_mapping(mapping_file):
mapping = {}
with open(mapping_file, 'r', encoding='utf-8') as file:
for line in file:
parts = line.strip().split(',')
if len(parts) == 2:
name, replacement = parts
mapping[name] = replacement
return mapping

# Replace text in the target file based on the mapping
def replace_text(target_file, mapping, output_file):
with open(target_file, 'r', encoding='utf-8') as file:
content = file.read()

for name, replacement in mapping.items():
content = content.replace(name, replacement)

with open(output_file, 'w', encoding='utf-8') as file:
file.write(content)

# Example usage
mapping_file = 'mapping.txt' # Name,Replacement (CSV format)
target_file = 'input.txt' # File where replacements happen
output_file = 'output.txt' # Modified file

mapping = load_mapping(mapping_file)
replace_text(target_file, mapping, output_fil
 
Joined
Mar 29, 2025
Messages
11
Reaction score
0
def replace_text_in_file(file_path, old_name, new_name):
with open(file_path, 'r') as file:
content = file.read()

content = content.replace(old_name, new_name)

with open(file_path, 'w') as file:
file.write(content)

# Usage
replace_text_in_file('file.txt', 'old_name', 'new_name')
 

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

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top