Python处理csv文档,最大值,最小值,求和,计数,等!!!
本帖最后由 Cool_Breeze 于 2020-5-20 17:21 编辑#!/usr/bin/env python3
#coding = utf-8
import os
#自己的格式打印类
from printf_ import printf_
file_path = r'C:\Users\user\Desktop\TWII'
os.chdir(file_path)
#没有按二级制读取,\r\n隐式转换为\n
f_csv = open('tes.csv','r')
#读取第一行标题
item = f_csv.readline().split(',')
#换行符为单独一列时,有效列数等于总列数-1
if item[-1] == '\n':
item_n = len(item) - 1
else:
#换行符\r\n与最后一列数据在同一个字符串中
item[-1] = item[-1]
item_n = len(item)
# print(each_line)
#初始化数据字典列表
data_item=dict(max=-10**20,max_time=None,min=10**20,min_time=None,avg=0,sum=0,count=0)
#创建对应空列表,用于每一列储存数据
item_list = []
for i in range(item_n):
item_list.append(data_item.copy())
# print(len(item_list))
#行数
line_count=0
#指定跳过数据列数(从左往右)0=1
skip_col = 0
#读余下取所有数据
for each_line in f_csv:
line_count += 1
# print(line_count)
temp = each_line.split(',')
temp_len = len(temp)
for i in range(temp_len):
#最后一列无效数据
if i <= skip_col or i == item_n: continue
#转换失败,t_data=0
try:
t_data = float(temp)
except ValueError:
t_data = 0
# 累加
item_list['sum'] += t_data
if t_data > item_list['max']:
item_list['max'] = t_data
item_list['max_time'] = temp
if t_data < item_list['min']:
item_list['min'] = t_data
item_list['min_time'] = temp
#avg赋值
for i in range(item_n):
if i <= skip_col or i == item_n: continue
# i >= temp_len-1说明最后一行数据不完整,影响平均值
if i > temp_len - 1:
item_list['avg'] = item_list['sum'] / (line_count - 1)
item_list['count'] = line_count - 1
else:
item_list['avg'] = item_list['sum'] / line_count
item_list['count'] = line_count
f_csv.close()
pf=printf_()
#根据默认标题名初始化列表宽度
sheet_len=
charlen = 0
#自动获取列表宽度
for i in item:
if len(i) > charlen: charlen = len(i)
sheet_len = charlen
temp_count = 0
for i in item_list:
if temp_count <= skip_col:
temp_count+=1
continue
for j,v in enumerate(list(i.values())):
charlen = len(str(v))
poi = j+1
if sheet_len < charlen: sheet_len = charlen
#获取标题列表
p_item=
p_item.insert(0,'item')
#打印+---+---+-+
pf.printf_head(*sheet_len)
#将2个列表合为一个列表(name,name长度),为了打印格式
#打印|xxx|xxx|x|
pf.printf_cnter(*list(row for row in zip(p_item,sheet_len) for i in range(2)))
pf.printf_head(*sheet_len)
for i in range(item_n):
if i <= skip_col or i == item_n: continue
#将字典值转换为列表
temp_list=list(item_list.values())
#将项目名添加进去
temp_list.insert(0,item)
pf.printf_cnter(*list(row for row in zip(temp_list,sheet_len) for i in range(2)))
pf.printf_head(*sheet_len)
+------------------------------------+------------+--------------+-----------+--------------+--------------------+--------------------+-------+
| item | max | max_time | min | min_time | avg | sum | count |
+------------------------------------+------------+--------------+-----------+--------------+--------------------+--------------------+-------+
|VDDCR_GFX Current (A) (A) | 244.95688| 20:32:05.864 | 2.00784 | 19:29:01.189 | 123.75535857904592 | 4502714.966540007| 36384 |
|GPU Temperature (C) (C) | 84.67364 | 22:06:03.904 | 36.74507| 19:29:01.189 | 80.63858083608046| 2933954.1251399517 | 36384 |
|Junction Temperature (C) (C)| 99.82796 | 22:06:07.134 | 36.78448| 19:29:01.189 | 93.10586805381416| 3387563.9032699745 | 36384 |
|Mem Temperature (C) (C) | 98.0 | 22:06:16.874 | 34.0 | 19:29:02.245 | 93.02386349137001| 3384580.2492700065 | 36384 |
|VR_GFX (C) (C) | 85.0 | 22:06:15.774 | 27.0 | 19:29:01.189 | 81.54814546119151| 2967047.724459992| 36384 |
|VR_SOC (C) (C) | 79.0 | 22:05:59.594 | 29.0 | 19:29:01.189 | 76.69735095179215| 2790556.4170300053 | 36384 |
|VR_MEM (C) (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|VR_VDDCI (C) (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|Liquid0 (C) (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|Liquid1 (C) (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PLX (C) (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|Min (C) (C) | 79.15897 | 22:06:02.803 | 35.35468| 19:29:01.189 | 75.73771405205596| 2755640.988070004| 36384 |
|GFXCLK Freq () | 2067.62036 | 05:42:38.450 | 804.09778 | 00:42:47.646 | 1917.2023946278628 | 69755491.92614016| 36384 |
|PWM () | 60.54811 | 22:06:21.194 | 0.0 | 19:29:01.189 | 48.997079181508425 | 1782709.7289400026 | 36384 |
|FAN Speed () | 2452.70947 | 22:06:26.614 | 0.0 | 19:29:01.189 | 2148.191614044644| 78159803.68540034| 36384 |
|Limit PPT0 (W) (W) | 195.0 | 19:29:01.189 | 195.0 | 19:29:01.189 | 195.0 | 7094880.0 | 36384 |
|Value PPT0 (W) (W) | 195.00354| 02:33:50.597 | 10.208 | 19:29:01.189 | 175.08616300241795 | 6370334.954679974| 36384 |
|GFX Activity (%) (%) | 99.9752 | 00:42:08.825 | 0.07353 | 00:51:53.411 | 85.77492298345469| 3120834.7978300154 | 36384 |
|PCIe Link Speed (GT/s) (GT/s) | 8.0 | 19:29:01.189 | 2.5 | 19:29:02.245 | 7.952685246262093| 289350.5 | 36384 |
|PCIe Link Width () | 16.0 | 19:29:01.189 | 16.0 | 19:29:01.189 | 16.0 | 582144.0 | 36384 |
|PCIe Correctable Error () | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PCIe Uncorrectable Error () | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PCIe Residency Gen 1 (%) (%)| 51.89874 | 19:29:09.790 | 2.52275 | 19:33:32.379 | 3.1485344538808073 | 114556.27756999929 | 36384 |
|PCIe Residency Gen 2 (%) (%)| 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PCIe Residency Gen 3 (%) (%)| 97.47725 | 19:33:32.379 | 48.10127| 19:29:09.790 | 96.85146558542264| 3523843.7238600175 | 36384 |
|PCIe Residency Gen 4 (%) (%)| 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PCIe Residency L0 (%) (%) | 100.0 | 19:29:01.189 | 99.9974 | 20:40:54.996 | 99.99928975374357| 3638374.158400206| 36384 |
|PCIe Residency L0s (%) (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|PCIe Residency L1 (%) (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
|Fan PWM reading [%] (%) | 60.0 | 19:32:11.820 | 0.0 | 19:29:01.189 | 48.993568601583114 | 1782582.0 | 36384 |
|mclk (MHz) | 32327430.0 | 01:14:48.400 | 101.0 | 19:29:01.189 | 1728.383577494984| 62883779.7 | 36383 |
|sclk (MHz) | 2084.0 | 22:58:23.259 | 796.0 | 19:56:22.313 | 1914.0741829975539 | 69639761.0 | 36383 |
+------------------------------------+------------+--------------+-----------+--------------+--------------------+--------------------+-------+
请按任意键继续. . . 本帖最后由 Cool_Breeze 于 2020-5-18 19:47 编辑
这个帖子是C语言的文件数据分析比较(Rev 02!)
比c简洁多了,只是慢了2秒左右! @_2_ @人造人 @hrp Cool_Breeze 发表于 2020-5-18 19:45
@_2_ @人造人 @hrp
过后尽量考虑一下 Python 的速度问题,
(小声bb:这是个大问题{:10_266:}) Cool_Breeze 发表于 2020-5-18 19:41
这个帖子是C语言的文件数据分析比较(Rev 02!)
比c简洁多了,只是慢了2秒左右!
你可以尝试 PrettyTable 进行漂亮打印,
总有人比你自己的轮子更好 _2_ 发表于 2020-5-18 20:08
你可以尝试 PrettyTable 进行漂亮打印,
总有人比你自己的轮子更好
恩恩!当时还不知道 PrettyTable,就当练习自己写了一个!
速度的确是大问题! Cool_Breeze 发表于 2020-5-18 20:14
恩恩!当时还不知道 PrettyTable,就当练习自己写了一个!
速度的确是大问题!
…… PyPi 里总有一些不知道用不用的上的轮子{:10_256:}
导致一个 Python 程序总会走向两条路线,
一个是啃官方文档总结的用法,一个是自造的轮子 Cool_Breeze 发表于 2020-5-18 19:45
@_2_ @人造人 @hrp
我不玩python的,^_^
为什么考虑CSV呢? 人造人 发表于 2020-5-18 22:04
我不玩python的,^_^
不好意思{:5_105:} hanjiang6607 发表于 2020-5-18 22:24
为什么考虑CSV呢?
还没有学。 _2_ 发表于 2020-5-18 20:08
你可以尝试 PrettyTable 进行漂亮打印,
总有人比你自己的轮子更好
还是的多看看库里面的东西。
学思路很重要!{:5_95:} 本帖最后由 hrp 于 2020-5-19 11:33 编辑
膜拜{:10_254:}
(小声:为啥@ 我不成功)
页:
[1]