-
Notifications
You must be signed in to change notification settings - Fork 14
/
convert_character_code.py
59 lines (45 loc) · 1.39 KB
/
convert_character_code.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
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding: euc-jp -*-
import sys
import chardet
import glob
import os.path
import codecs
if __name__ == '__main__':
args = sys.argv
dirpath = args[1]
target = args[2]
code = args[3]
ext_list = ["*.cpp",
"*.h"]
path_list = ["src/lib/rtm",
"src/lib/coil",
"src/lib/coil/common",
os.path.join("src/lib/coil", target, "coil"),
"examples/*",
"utils/*",
"src/ext/*/*"]
#path_list = [os.path.join("src/lib/coil", target, "coil")]
for p in path_list:
for e in ext_list:
#print os.path.join(p, e)
#print glob.glob(os.path.join(dirpath, p, e))
filelist = glob.glob(os.path.join(dirpath, p, e))
for n in filelist:
with open(n, mode='rb') as f:
encoding = chardet.detect(f.read())["encoding"]
if encoding is None:
encoding = "utf_8_sig"
elif encoding.lower() == "utf-8":
encoding = "utf_8_sig"
str_list = []
if encoding.lower() != code.lower():
try:
with codecs.open(n, "r", encoding) as f:
for row in f:
str_list.append(row)
with codecs.open(n, "w", code) as f:
for row in str_list:
f.write(row)
except:
pass