python文件夹和表格处理

1,读取python中的第一层的文件夹和第二层的文件夹

import os
import json

path="/home/SENSETIME/huangweijie2/PycharmProjects/AUTODRIVE-7644_get_diff_map_node_score_ab2a7f4_2021_03_15_21_48_15"

def get_sub_dirs(root_path):
    root_depth = len(root_path.split(os.path.sep))
    firt_lever=[]
    folder_name=[]
    for root, dirs, files in os.walk(root_path, topdown=True):
        for name in dirs:
            dir_path = os.path.join(root, name)
            dir_depth = len(dir_path.split(os.path.sep))
            if dir_depth == root_depth + 1:
                firt_lever.append(dir_path)
                folder_name.append(name)
            else:
                break
    return firt_lever,folder_name

json_name="evaluation_result.json"

first,folders=get_sub_dirs(path)
for folder in folders:
    print(folder)

2,导出的excel表格根据内容设置表的高度和宽度
表格读取
自适应列宽
表格格式处理

3,txt中每行有多个txt的时候,使用numpy直接读取就可以了

a=np.loadtxt(txt_name)

4,python画出分位图

import matplotlib.pyplot as plt
fig = plt.figure()  # 创建画布
ax = plt.subplot()  # 创建作图区域
# 蓝色矩形的红线:50%分位点是4.5,上边沿:25%分位点是2.25,下边沿:75%分位点是6.75
ax.boxplot([range(5), range(10), range(20)])
plt.show()

5,多个文件时候的操作

import pandas
import glob
 
for r in glob.glob("test*.csv"):
    csv=pandas.read_csv(r)
    csv.to_csv("test.txt",mode="a+")

6,批量的更换文件的后缀名

def rename_type_as_json(path, type='.json'):
    filelist = os.listdir(path)
    for files in filelist:
        olddir = os.path.join(path, files)
        if os.path.isdir(olddir):
            continue
        filename = os.path.splitext(files)[0]
        filetype = os.path.splitext(files)[1]
        newdir = os.path.join(path, filename + type)
        os.rename(olddir, newdir)
        return
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值