Skip to content

Commit 8bbca11

Browse files
committed
add py files
1 parent 2b4401c commit 8bbca11

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tqdm
2+
click
3+
pandas

tdx2csv.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
"""Main module."""
3+
4+
import codecs as cs
5+
import os
6+
from glob import glob
7+
8+
import click
9+
import pandas as pd
10+
from tqdm import tqdm
11+
12+
13+
def txt2csv(txt, output):
14+
with cs.open(txt,'r', 'gbk') as f:
15+
fl = f.readlines()
16+
17+
del fl[0]
18+
del fl[0]
19+
del fl[-1]
20+
21+
if not os.path.isdir(output):
22+
os.mkdir(output)
23+
24+
csv = os.path.split(txt)
25+
csv = csv[1].replace('.txt', '.csv')
26+
csv = os.path.join(output, csv)
27+
28+
with open(csv, 'w') as fp:
29+
fp.writelines(fl)
30+
31+
df = pd.read_csv(csv, names=['Date','Open','High','Low','Close','Volume','Amount'])
32+
df.to_csv(csv, index=False)
33+
34+
@click.command(help='通达信数据转换程序.')
35+
@click.option('-i', '--input', default=os.path.expanduser("./input"), type=click.Path(file_okay=False), help='历史数据下载目录, 默认当前目录下 input')
36+
@click.option('-o', '--output', default=os.path.expanduser("./output"), type=click.Path(file_okay=False), help='转换后输出目录, 默认当前目录下 output')
37+
def main(input, output):
38+
files = glob('%s/*.txt' % input.rstrip('/'))
39+
for txt in tqdm(files):
40+
txt2csv(txt, output)
41+
42+
43+
if __name__ == '__main__':
44+
main()

0 commit comments

Comments
 (0)