-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
62 lines (54 loc) · 1.75 KB
/
format.py
File metadata and controls
62 lines (54 loc) · 1.75 KB
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
60
61
62
# coding = UTF-8
import os
import sys
import pandas as pd
import fnmatch
def formatting():
with open(filesource, encoding='UTF-8') as file:
name = file.name.rstrip('.txt')
keys = name.lstrip(r'.\FileSource/')
key = []
text = []
# print(name)
# print(keys)
keynum = 1
for line in file.readlines():
line = line.strip()
key.append(rf'{keys}_line{keynum}')
text.append(line)
keynum = keynum + 1
paragraph = {'keys(脚本冗余,无需翻译)': key, 'text(脚本冗余,无需翻译)': text}
data = pd.DataFrame(paragraph)
formatdata = data
# print(data)
formatdata.to_csv(f'{name}.csv', index=False)
file.close()
if __name__ == "__main__":
print('txt 转 csv工具 for Paratranz, Copyright 2022 ritenseki.')
print('ritenseki on Github: https://github.com/ritenseki')
# print('本工具免费提供。')
print('开始转换?')
inputting = input('[Y/n]')
if inputting == 'Y':
try:
sourcedirs = os.listdir('.\FileSource')
except FileNotFoundError as error:
print('找不到FileSource目录,是否新建?')
inp = input('[Y/n]')
if inp == 'Y':
os.makedirs('FileSource')
else:
sys.exit()
else:
for f in sourcedirs:
if fnmatch.fnmatch(f, '*.txt'):
filesource = rf'.\FileSource/{f}'
# print(filesource)
formatting()
print('Done!')
finally:
i = input("按任意键继续")
sys.exit()
else:
i = input("按任意键继续")
sys.exit()