Page 1 of 1

Fügen Sie den Text ersetzen Sie den Text basierend auf einem Namen in einem anderen Skript für Datei -Python -Skript

Posted: 04 Mar 2025, 14:35
by Anonymous
Ich habe zwei Textdateien, Datei 1 ist eine Liste von Namen von Maschen mit einem Pfad. Manchmal ist der Name der gleiche Pfadname und kann Zahlen enthalten. Die Zeilen haben immer staticMesh = staticMesh'Replaceword '
Ich habe ein Python -Skript, aber ich erhalte einen weiteren Fehler. Außerdem möchte ich in eine Datei schreiben < /p>

Code: Select all

Traceback (most recent call last):
File "C:\Users\TUFGAMING\Desktop\python\main.py", line 32, in 
main()
File "C:\Users\TUFGAMING\Desktop\python\main.py", line 20, in main
quoted_word = match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
< /code>
Hier ist ein Beispiel des Datenblocks < /p>
(Datei 1) < /p>
Dog /Game/Meshes/Bulldog

Fish /Game/Meshes/Goldfish

Cat /Game/Meshes/Cat
< /code>
(Datei 2) < /p>
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
< /code>
(Ergebnis) < /p>
StaticMesh=StaticMesh'/Game/Meshes/Bulldog'

StaticMesh=StaticMesh'/Game/Meshes/Goldfish'

StaticMesh=StaticMesh'/Game/Meshes/Cat'
< /code>
import re

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

# holds (firstword, secondline)
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]))

with open(file_two, 'r') as file:
for line in file:
# extract content between the single quotes
match = re.search(r"StaticMesh=?'(.+)'", line)
quoted_word = match.group(1)

# see if any of the lines from file 1 match it
for word_tuple in word_tuples:
if word_tuple[0] == quoted_word:
print("MATCH FOUND")
print(f"file1: {word_tuple[0]} {word_tuple[1]}")
print(f"file2: {line.strip()}")
print(f"file3: {line}".replace(quoted_word, word_tuple[1]))

if __name__ == '__main__':
main()