Text mit Umbruch einfügen
Verfasst: Freitag 2. Februar 2018, 17:03
Halli, ich bin dabei ein Programm für die Übersetzung von Häkelanleitungen zu machen. Bis jetzt ist es so, dass ich beim eingefügten Text (Anleitungstext) keinen Zeilenumbruch haben kann. Sonst kommt ein Fehler. Kann man es so schreiben, dass ich den Text einfach kopiere und die Zeilenumbrüche akzeptiert werden? Falls ja: wie?
Code: Alles auswählen
import re
Anleitungstext = "Round 1: 2 ch, 6 sc in second chain from hook [6]Round 2: inc 6 times [12]Round 3: (sc in next st, inc) repeat 6 times [18]Round 4: (sc in next 2 st, inc) repeat 6 times [24]Round 5: (sc in next 3 st, inc) repeat 6 times [30]Round 6: (sc in next 4 st, inc) repeat 6 times [36]Round 7: sc in next 15 st, inc 6 times, sc in next 15 st [42]Round 8: sc in next 15 st, dec 6 times, sc in next 15 st [36]Round 9: sc in all 36 st [36]Round 10: sc in next 12 st, dec 6 times, sc in next 12st [30]Round 11-13: sc in all 30 st [30]Round 14: (sc in next 8 st, dec) repeat 3 times [27]Round 15: sc in all 27 st [27]Round 16: (sc in next 7 st, dec) repeat 3 times [24]Round 17: sc in all 24 st [24]Round 18: (sc in next 2 st, dec) repeat 6 times [18]Round 19: sc in all 18 st [18]Round 20: (sc in next st, dec) repeat 6 times [12]Round 21-22: sc in all 12 st [12]Fasten off leaving a tail for sewing. Stuff. " ""
translation_table = {'Rnd 2: (inc) repeat 6 times [12]':'jede Masche verdoppeln',
'repeat 6 times':'6x wiederholen',
'2sc in each':'Jede Masche verdoppeln',
'in a magic ring':'in einen Fadenring',
'sc in the next st, inc':'jede 2. Masche verdoppeln ',
'sc in the next 2 st, inc':'jede 3. Masche verdoppeln',
'sc in the 3 st,inc':'jede 4. Masche verdoppeln',
'sc':'fM',
'inc':'Zun',
'in next st':'in die nächste/n Maschen',
'in next 2 st':'jede 2.Mache verdoppeln',
'in next 8 st':'in die nächsten 8 Maschen',
'(sc in the next 2 st), inc' : 'jede 3. Masche verdoppeln',
'sc in magic ring':'fM in den Fadenring',
'in the next':'in',
'sl st' :'KM',
'in all' : 'in alle',
'st':'Maschen',
'in next':'',
'dec':'Abn',
'repeat 3 times':'3x wiederholen',
'repeat 4 times' : '4x wiederholen',
'repeat 2 times' : '2x wiederholen',
'fM in all 18 st':'fM in alle 18 Maschen',
'Round':'Runde'
}
pattern = re.compile(r'\b(' + '|'.join(translation_table.keys()) +r')\b')
result = pattern.sub(lambda x : translation_table[x.group()], Anleitungstext)
print(result)