#🔒 My skript does it what it does, except it puts an " to the end of the lineor behind the comma

18 messages · Page 1 of 1 (latest)

robust mauveBOT
#

@stiff mountain

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stiff mountain
#

My skript does it what it does, except it puts an " to the end of the line or behind the comma

#
def replace_strings(file_a, file_b):
    translations = {}
    # Lesen der Übersetzungen aus Datei B und speichern sie in einem Wörterbuch
    with open(file_b, 'r') as f_b:
        for line in f_b:
            line = line.strip()
            if line.startswith('"'):
                parts = line.split('": "')
                if len(parts) == 2:
                    translations[parts[0][1:]] = parts[1][:-1]

    # Durchsuchen von Datei A und Ersetzen der Übersetzungen
    with open(file_a, 'r') as f_a:
        content_a = f_a.readlines()

    # Bearbeiten der Zeilen in Datei A
    for i, line in enumerate(content_a):
        line = line.strip()
        if line.startswith('"'):
            parts = line.split('": "')
            if len(parts) == 2 and parts[0][1:] in translations:
                content_a[i] = f'"{parts[0][1:]}": "{translations[parts[0][1:]]}",\n'
            else:
                content_a[i] = f'{line.rstrip()[:-1]},\n'

    # Schreiben der aktualisierten Inhalte zurück in Datei A
    with open(file_a, 'w') as f_a:
        for line in content_a:
            f_a.write(line)

    print("Übersetzungen wurden erfolgreich in Datei A übernommen.")

replace_strings(r"C:\Users\Wento\Documents\Python Skripte\Für Übersetzungen\en_US.json", r"C:\Users\Wento\Documents\Python Skripte\Für Übersetzungen\de_DE.json")```
patent star
#

I very strongly recommend using the stdlib json module rather than trying to process json by hand

#

It can parse the file into an object tree for you, rather than you needing to sift through for ": " and such constructs

stiff mountain
#

do i have to rewrite the code for it ?

#

or just simple corrections

vernal arch
#

it can convert python objects to JSON objects and vice versa

#

so it would replace the part of your code that is trying to do that

stiff mountain
#

@patent starthanks

#

@vernal archok

#

ty

#

It did work now, thank you
i will close this topic

robust mauveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

🔒 My skript does it what it does, except it puts an " to the end of the line or behind the comma

robust mauveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.