# coding: utf-8
import os
import pandas as pd
import numpy as np
import re
def scan_files(directory, prefix=None, postfix=None):
files_list = []
for root, sub_dirs, files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
files_list.append(os.path.join(root, special_file))
elif prefix:
if special_file.startswith(prefix):
files_list.append(os.path.join(root, special_file))
else:
files_list.append(os.path.join(root, special_file))
return files_list
if __name__ == '__main__':
directory = '/home/companyshare/研发部/data/future_tick'
for files in scan_files(directory):
if files != '/home/companyshare/研发部/data/future_tick/hdf' and files != '/home/companyshare/研发部/data/future_tick/raw':
if files.find('.csv')!=-1:
try:
df = pd.read_csv(open(r'{0}'.format(files), encoding='GBk'))
files_e = files.split('/')
files_e.insert(6, 'hdf')
total_str = ''
for x in files_e:
total_str += x + '/'
total_str = total_str.replace('.csv/', '.hdf')
if os.path.exists(os.path.dirname(total_str)) == False:
os.makedirs(os.path.dirname(total_str))
df.to_hdf(total_str, 'df',mode='w')
print("successful"+total_str)
except:
print(files)
continue
python csv 转 hdf
最新推荐文章于 2023-08-11 16:44:26 发布