import re import sys def repl(char): x = char.group(0) output_str = "" for c in x: c = c.swapcase() output_str += c return output_str pattern = re.compile(r"\b[a-ząćęłńóśźż]+[A-ZĄĆĘŁŃÓŚŹŻ]+\b|\b[A-ZĄĆĘŁŃÓŚŹŻ]+[a-ząćęłńóśźż]+\b") # f = open("polish_wiki_excerpt.in", "r") def change_line(word_list, curr_line): for word in word_list: curr_line = curr_line.replace(word, word.swapcase()) return curr_line.rstrip('\n') for line in open("./polish_wiki_excerpt.in", encoding="utf8"): # for line in sys.stdin: found_words = re.findall(pattern, line) out = change_line(found_words, line) if out: print(out)