python数据分析--文件读写

import glob
import pandas as pd


filelocation="F:\\"    #在哪里搜索多个表格   
fileform="xlsx"       #当前文件夹下搜索的文件名后缀  
  
#首先查找默认文件夹下有多少文档需要整合  
filearray=[]  
for filename in glob.glob(filelocation+"*."+fileform):  
    filearray.append(filename)  
#以上是从pythonscripts文件夹下读取所有excel表格,并将所有的名字存储到列表filearray


print("在默认文件夹下有%d个文档"%len(filearray)) 
ge=len(filearray)  
matrix = [None]*ge 
 

all_data = pd.DataFrame()  #定义空的DataFrame,接收数据
#读取数据
for i in range(ge):
   print(i)
   l_data = pd.read_excel(filearray[i])
   all_data = all_data.append(l_data)
def csv_new(storage):
    csv_storage = []
    with codecs.open(storage, 'r', encoding='utf-8') as fp:
        fp_key = csv.reader(fp)
        for csv_key in fp_key:
            csv_reader = csv.DictReader(fp, fieldnames=csv_key)
            for row in csv_reader:
                csv_dict = dict(row)
                csv_storage.append(csv_dict)
    print(len(csv_storage))
 
if __name__ == '__main__':
    csv_new('test.csv')
#深度学习
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
np.set_printoptions(threshold=np.inf)  # 设置打印出所有参数,不要省略

案例:提取文件夹下所有图片都路径

使用模块:pathlib 

import pathlib

data_dir='./2_class'
data_root=pathlib.Path(data_dir)

#data_root.iterdir() 迭代目录
all_dir=[str(dir) for dir in data_root.iterdir()]

#data_root.glob()  迭代目录下所有文件
all_image_path=[str(path) for path in data_root.glob('*/*')]

在做深度学习时,需要将图片乱序:

import random
random.shuffle(all_image_path)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值