python 同时载入多个数据文件

来源:互联网 发布:代理淘宝开店 编辑:程序博客网 时间:2024/05/24 06:40

首先将要载入的文件全放入一个文件夹,比如:D:\Traverse_document


然后写入代码:

# coding: utf-8import osimport json#给定文件夹路径,获取文件夹下所有文件的文件名称def VisitDir(path):    document_file=[]    for root,dirs,files in os.walk(path):#root读路径,files读路径下文件夹名字,dirs是嵌套文件夹时用,此处不需要用       for filename in files:          b=os.path.join(root,filename)          document_file.append(b)    return document_file#将获取的文件名称传入loaddata函数,批量导入这些文件def Loaddata(document_file):    list=[]    for i in document_file:        f=open(i)        f=json.load(f)        list.append(f)    return listif __name__=="__main__": path="D:\\Traverse_document" document_file=VisitDir(path) print document_file    #目录下所有文件列表 list=Loaddata(document_file) print list[1]["id"]     #list[0]为一号文件夹内容、list[1]为第二个文件夹内容····依次类推

同理可以同时载入多个csv文件

1 0
原创粉丝点击