Ich arbeite gerade an einen Übersetzer, der amerikanisches Englisch (AE) in britisches Englisch (BE) übersetzt. Um dies umzusetzen bin ich wie folgt vorgegangen:
Zuerst habe ich von der Seite http://www.tysto.com/uk-us-spelling-list.html die Übersetzungen in zwei Spalten in der Excel Datei "Wörterbuch.xlsx" übertragen.
Die zu übersetzenden Wörter befinden sich in "wörter.xlsx". Das Ergebnis lautet dann "translated.xlsx".
Aus Wörterbuch.xlsx entsteht ein Dataframe namens df_woerterbuch. Mithilfe dieses Dataframes wird die dictionary AE_BE_worterbuch erstellt. US Wörter sind die Keys (Index= 0). BE Wörter (Index=1) sind die Values.
Ich habe das Problem, dass ich nicht weiss, warum der folgende Fehler auftritt:
Code: Alles auswählen
UK US
0 accessorise accessorize
1 accessorised accessorized
2 accessorises accessorizes
3 accessorising accessorizing
4 acclimatisation acclimatization
Traceback (most recent call last):
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:/wörterbuchabgleich.py", line 22, in <module>
AE_BE_woerterbuch = dict(zip(df_woerterbuch[0], df_woerterbuch[1]))
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
Process finished with exit code 1
Mein Python Code lautet wie folgt: (Hinweis: Die Pakete xlrd und openpyxl müssen installiert sein, damit auf xlsx zugegriffen werden kann)
Code: Alles auswählen
#install xlrd
#install openpyxl
import pandas as pd
woerter = pd.read_excel(r'F:\PFAD\wörter.xlsx',
header=0,
usecols=['American_English'])
df_woerterbuch = pd.read_excel(r'F:\PFAD\Wörterbuch.xlsx',
header=0,
usecols=[0, 1])
AE_BE_woerterbuch = dict(zip(df_woerterbuch[0], df_woerterbuch[1]))
translation=[]
for index, row in woerter.iterrows():
AE_type=str(row['American_English'])
BE_type=AE_BE_woerterbuch.get(AE_type.lower())
translation.append({'AE_Type': str(AE_type), 'BE_Type': str(BE_type)})
translations = pd.DataFrame(data=translation, columns=['AE_Type','BE_Type'])
translations.to_excel('translated.xlsx')