Home > Mobile >  How to open and read multiple files (ex. .txt) in Python
How to open and read multiple files (ex. .txt) in Python

Time:08-16

As an introduction to Python, i was tasked to code a simple German to English as well as English to German translator. Both .txt files are written like:

Italy Italien
Germany Deutschland
Austria Österreich
...

Here's my code:

woerter = {}
fobj = open("woerterbuch.txt", "r") # and "woerterbuch2.txt"
for line in fobj:
    line = line.strip()
    zuordnung = line.split(" ")
    woerter[zuordnung[0]] = zuordnung[1]
fobj.close()
print("Um das Spiel zu beenden geben Sie 'Ende' ein!")
while True:
    wort = input("Geben Sie ein Wort ein: ")
    if wort in woerter: # if word in words
        print("Das deutsche Wort lautet:", woerter[wort]) # the german word is:
    elif wort == "Ende":
        break
    else:
        print("Das Wort ist unbekannt!") # Word unknown

I have two separate .txt files that I want to implement in this code. However, with my current knowledge I can only implement one. As soon as I try to code the other one in, the first woerterbuch.txt does not get registered. Currently you can only input the English words for the German translation. If you could show me how to input the German word for the English translation, I would be glad!

CodePudding user response:

If you have the "pandas" module, you could build your language translation file like a "CSV" file and then build a dictionary from that file. Following is a" proof of principle" snippet of code that should meet the spirit of your project.

import pandas as pd

woerter = {}

df=pd.read_csv('Words.csv')

list_english = df['English'].tolist()
list_deutsch = df['Deutsch'].tolist()
    
for i in range(0, len(list_english)):
    woerter[list_english[i]] = list_deutsch[i]  # English to German
    woerter[list_deutsch[i]] = list_english[i]  # Deutsch nach Englisch
    
print(woerter)

print("Um das Spiel zu beenden geben Sie 'Ende' ein!")
while True:
    wort = input("Geben Sie ein Wort ein: ")
    if wort == "Ende":
        break
    if wort in woerter: # if word in words
        print("Das übersetzte wort ist:", woerter[wort]) # the translated word is:
    else:
        print("Das Wort ist unbekannt!") # Word unknown

Using that a simple word/phrase pair file could be set up.

English,Deutsch
Hello,Guten Tag
Goodbye,Auf Wiedersehen
cold,kalt
hot,heiss
blue,blau
small,klien
excuse me,
thanks,vielen dank
etcetara,und so weiter

That way, the pairs are only entered once, the dictionary can be built to be bi-directional.

Here was some sample testing on my terminal.

@Una:~/Python_Programs/Translate$ python3 Translate.py 
{'Hello': 'Guten Tag', 'Guten Tag': 'Hello', 'Goodbye': 'Auf Wiedersehen', 'Auf Wiedersehen': 'Goodbye', 'cold': 'kalt', 'kalt': 'cold', 'hot': 'heiss', 'heiss': 'hot', 'blue': 'blau', 'blau': 'blue', 'small': 'klien', 'klien': 'small', 'excuse me': verzeihung, verzeihung: 'excuse me', 'thanks': 'vielen dank', 'vielen dank': 'thanks', 'etcetara': 'und so weiter', 'und so weiter': 'etcetara'}
Um das Spiel zu beenden geben Sie 'Ende' ein!
Geben Sie ein Wort ein: Hello
Das deutsche Wort lautet: Guten Tag
Geben Sie ein Wort ein: Guten Tag
Das deutsche Wort lautet: Hello
Geben Sie ein Wort ein: etcetera
Das Wort ist unbekannt!
Geben Sie ein Wort ein: etcetara
Das deutsche Wort lautet: und so weiter
Geben Sie ein Wort ein: Ende

Give that a try.

CodePudding user response:

If both have English->German, then you simply need to plop your file reading code in a loop that loops over all the files you want to read. If the language is flipped, you'll need separate blocks of code to handle this.

To make things easier, let's define a function that will read a file, and return a dictionary.

def read_dictionary_file(file_name):
    woerter = {}
    fobj = open(file_name, "r")
    for line in fobj:
        line = line.strip()
        zuordnung = line.split()
        woerter[zuordnung[0]] = zuordnung[1]
    fobj.close()
    return woerter

Sidebar: since the only thing done by the loop in the function above is build the dictionary, it can be converted to a one-liner using the dict constructor like so:

def read_dictionary_file(file_name):
    with open(file_name, "r") as fobj:
        woerter = dict(line.strip().split() for line in fobj)
    return woerter

Now, if all your files are English->German, you just need to call this function multiple times to build your dictionary.

en_de = dict()

file_names = ["woerterbuch.txt", "woerterbuch2.txt"]
for file_name in file_names:
    file_words = read_dictionary_file(file_name)
    en_de.update(file_words)

At the end of this, you should have a dictionary en_de which contains keys that are English words, and their values are the respective German translations.

Now, since the whole point of a dictionary is to enable fast lookup, we will need to create the reversed dictionary -- German to English -- for that translation, instead of iterating over all the values in en_de to find the German word we want. To do this, use the following:

def reverse_dictionary(input_dict):
    return {v: k for k, v in input_dict.items()}

de_en = reverse_dictionary(en_de)

At this point, you should have two dictionaries -- one English to German, the other German to English. Now, you can try to find the input word in either dictionary and print the output.

inp = input("Enter your word: ")
if inp in en_de or inp in de_en:
    if inp in en_de:
        translation = en_de[inp]
        print(f"(EN) {inp} == {translation} (DE)")

    if inp in de_en:
        translation = en_de[inp]
        print(f"(DE) {inp} == {translation} (EN)")
else:
    print("Word unknown")

Now, if your two files contain different order, i.e. one contains EN->DE and the other contains DE->EN, you can still use these functions we defined, but slightly differently.

en_de = read_dictionary_file("woerterbuch.txt")
de_en = read_dictionary_file("woerterbuch2.txt")

If your EN->DE dictionary contains words that are not in your DE->EN dictionary, and you want to reverse it, you can do:

en_de2 = reverse_dictionary(de_en)
de_en2 = reverse_dictionary(en_de)

en_de.update(en_de2)
de_en2.update(de_en2)

The code to translate the user's input remains the same.

  • Related