python读取几个g的csv_Python Pandas-读取包含多个表的csv文件

熊猫似乎并没有准备好轻易地做到这一点,所以我最终完成了自己的split_csv功能。它只需要表名,并将输出以每个表命名的.csv文件。import csv

from os.path import dirname # gets parent folder in a path

from os.path import join # concatenate paths

table_names = ["Inventory", "HP BladeSystem Rack", "Network Interface"]

def split_csv(csv_path, table_names):

tables_infos = detect_tables_from_csv(csv_path, table_names)

for table_info in tables_infos:

split_csv_by_indexes(csv_path, table_info)

def split_csv_by_indexes(csv_path, table_info):

title, start_index, end_index = table_info

print title, start_index, end_index

dir_ = dirname(csv_path)

output_path = join(dir_, title) + ".csv"

with open(output_path, 'w') as output_file, open(csv_path, 'rb') as input_file:

writer = csv.writer(output_file)

reader = csv.reader(input_file)

for i, line in enumerate(reader):

if i < start_index:

continue

if i > end_index:

break

writer.writerow(line)

def detect_tables_from_csv(csv_path, table_names):

output = []

with open(csv_path, 'rb') as csv_file:

reader = csv.reader(csv_file)

for idx, row in enumerate(reader):

for col in row:

match = [title for title in table_names if title in col]

if match:

match = match[0] # get the first matching element

try:

end_index = idx - 1

start_index

except NameError:

start_index = 0

else:

output.append((previous_match, start_index, end_index))

print "Found new table", col

start_index = idx

previous_match = match

match = False

end_index = idx # last 'end_index' set to EOF

output.append((previous_match, start_index, end_index))

return output

if __name__ == '__main__':

csv_path = 'switch_records.csv'

try:

split_csv(csv_path, table_names)

except IOError as e:

print "This file doesn't exist. Aborting."

print e

exit(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值