-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
50 lines (39 loc) · 1.18 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Convert letters to ASCI chars
"""
import sys
polish_letters="ąć곣ńŃ󜌿ŻźŹ"
asci_letters = "acelLnNosSzZzZ"
def main():
if len(sys.argv) != 2:
print("Give the full path to file..")
sys.exit(1)
return sys.argv[1]
def replaceLetters(line):
for pl, al in zip(polish_letters, asci_letters):
line = line.replace(pl, al)
return line
def readFile(file_name):
# open and add in memory
with open(f"{file_name}", "rt") as fin:
inmemory_file = fin.read()
return inmemory_file
def execute(file_name, inmemory_file):
# read from memory and change
with open(f"{file_name}", "wt") as fout:
for line in inmemory_file:
fout.write(replaceLetters(line))
return "Letters converted.."
# [optional]
# dynamic changes (requires two files)
# not possible to read and write changes in one file on the fly
# fin = open("/tmp/dupa/data.txt", "rt")
# fout = open("/tmp/dupa/out.txt", "wt")
# for line in fin:
# fout.write(replace_letters(line))
# fin.close()
# fout.close()
if __name__ == '__main__':
file_name = main()
inmemory_file = readFile(file_name)
print(execute(file_name, inmemory_file))