-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-3to2.py
28 lines (23 loc) · 1.01 KB
/
convert-3to2.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
import re
import os
def clone_py2(py_file):
with open(py_file, "r") as f:
contents = f.read()
contents = contents.split("\n")
contents = [re.sub("import csv", "import unicodecsv as csv", i)
for i in contents]
contents = [re.sub("from urllib.request", "from urllib", i)
for i in contents]
print_index = [i for i, word in enumerate(contents) if
re.match(".+print.+", word)]
print_fix = [re.sub(",", " + ", re.sub("[\(\)]", " ", contents[i]))
for i in print_index]
for i, v in enumerate(print_index):
contents[v] = print_fix[i]
os.makedirs(os.getcwd() + "/py2/", exist_ok=True)
with open(os.getcwd() + "/py2/py2_" + py_file, "w") as w:
for line in contents:
w.write("%s\n" % line)
py_files = list(filter(lambda x: x.startswith("prices"), os.listdir()))
for py_file in py_files:
clone_py2(py_file)