标题: [原创代码] Python处理csv文档,最大值,最小值,求和,计数,等!!! [打印本页]
作者: Gin_Q 时间: 2020-5-18 19:51 标题: Python处理csv文档,最大值,最小值,求和,计数,等!!!
本帖最后由 Gin_Q 于 2020-5-20 17:17 编辑
- #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][0:-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[i])
- except ValueError:
- t_data = 0
- # 累加
- item_list[i]['sum'] += t_data
- if t_data > item_list[i]['max']:
- item_list[i]['max'] = t_data
- item_list[i]['max_time'] = temp[0]
- if t_data < item_list[i]['min']:
- item_list[i]['min'] = t_data
- item_list[i]['min_time'] = temp[0]
- #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[i]['avg'] = item_list[i]['sum'] / (line_count - 1)
- item_list[i]['count'] = line_count - 1
- else:
- item_list[i]['avg'] = item_list[i]['sum'] / line_count
- item_list[i]['count'] = line_count
- f_csv.close()
-
- pf=printf_()
- #根据默认标题名初始化列表宽度
- sheet_len=[4,3,8,3,8,3,3,5]
- charlen = 0
- #自动获取列表宽度
- for i in item:
- if len(i) > charlen: charlen = len(i)
- sheet_len[0] = 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[poi] < charlen: sheet_len[poi] = charlen
-
- #获取标题列表
- p_item=[i for i in data_item.keys()]
- p_item.insert(0,'item')
-
- #打印+---+---+-+
- pf.printf_head(*sheet_len)
-
- #将2个列表合为一个列表(name,name长度),为了打印格式
- #打印|xxx|xxx|x|
- pf.printf_cnter(*list(row[i] 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[i].values())
- #将项目名添加进去
- temp_list.insert(0,item[i])
- pf.printf_cnter(*list(row[i] 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) [0] (A) | 244.95688 | 20:32:05.864 | 2.00784 | 19:29:01.189 | 123.75535857904592 | 4502714.966540007 | 36384 |
- | GPU Temperature (C) [0] (C) | 84.67364 | 22:06:03.904 | 36.74507 | 19:29:01.189 | 80.63858083608046 | 2933954.1251399517 | 36384 |
- | Junction Temperature (C) [0] (C) | 99.82796 | 22:06:07.134 | 36.78448 | 19:29:01.189 | 93.10586805381416 | 3387563.9032699745 | 36384 |
- | Mem Temperature (C) [0] (C) | 98.0 | 22:06:16.874 | 34.0 | 19:29:02.245 | 93.02386349137001 | 3384580.2492700065 | 36384 |
- | VR_GFX (C) [0] (C) | 85.0 | 22:06:15.774 | 27.0 | 19:29:01.189 | 81.54814546119151 | 2967047.724459992 | 36384 |
- | VR_SOC (C) [0] (C) | 79.0 | 22:05:59.594 | 29.0 | 19:29:01.189 | 76.69735095179215 | 2790556.4170300053 | 36384 |
- | VR_MEM (C) [0] (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | VR_VDDCI (C) [0] (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | Liquid0 (C) [0] (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | Liquid1 (C) [0] (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PLX (C) [0] (C) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | Min (C) [0] (C) | 79.15897 | 22:06:02.803 | 35.35468 | 19:29:01.189 | 75.73771405205596 | 2755640.988070004 | 36384 |
- | GFXCLK Freq [0] () | 2067.62036 | 05:42:38.450 | 804.09778 | 00:42:47.646 | 1917.2023946278628 | 69755491.92614016 | 36384 |
- | PWM [0] () | 60.54811 | 22:06:21.194 | 0.0 | 19:29:01.189 | 48.997079181508425 | 1782709.7289400026 | 36384 |
- | FAN Speed [RPM] [0] () | 2452.70947 | 22:06:26.614 | 0.0 | 19:29:01.189 | 2148.191614044644 | 78159803.68540034 | 36384 |
- | Limit PPT0 (W) [0] (W) | 195.0 | 19:29:01.189 | 195.0 | 19:29:01.189 | 195.0 | 7094880.0 | 36384 |
- | Value PPT0 (W) [0] (W) | 195.00354 | 02:33:50.597 | 10.208 | 19:29:01.189 | 175.08616300241795 | 6370334.954679974 | 36384 |
- | GFX Activity (%) [0] (%) | 99.9752 | 00:42:08.825 | 0.07353 | 00:51:53.411 | 85.77492298345469 | 3120834.7978300154 | 36384 |
- | PCIe Link Speed (GT/s) [0] (GT/s) | 8.0 | 19:29:01.189 | 2.5 | 19:29:02.245 | 7.952685246262093 | 289350.5 | 36384 |
- | PCIe Link Width [0] () | 16.0 | 19:29:01.189 | 16.0 | 19:29:01.189 | 16.0 | 582144.0 | 36384 |
- | PCIe Correctable Error [0] () | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PCIe Uncorrectable Error [0] () | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PCIe Residency Gen 1 (%) [0] (%) | 51.89874 | 19:29:09.790 | 2.52275 | 19:33:32.379 | 3.1485344538808073 | 114556.27756999929 | 36384 |
- | PCIe Residency Gen 2 (%) [0] (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PCIe Residency Gen 3 (%) [0] (%) | 97.47725 | 19:33:32.379 | 48.10127 | 19:29:09.790 | 96.85146558542264 | 3523843.7238600175 | 36384 |
- | PCIe Residency Gen 4 (%) [0] (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PCIe Residency L0 (%) [0] (%) | 100.0 | 19:29:01.189 | 99.9974 | 20:40:54.996 | 99.99928975374357 | 3638374.158400206 | 36384 |
- | PCIe Residency L0s (%) [0] (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | PCIe Residency L1 (%) [0] (%) | 0.0 | 19:29:01.189 | 0.0 | 19:29:01.189 | 0.0 | 0.0 | 36384 |
- | Fan PWM reading [%] [0] (%) | 60.0 | 19:32:11.820 | 0.0 | 19:29:01.189 | 48.993568601583114 | 1782582.0 | 36384 |
- | mclk[0] (MHz) | 32327430.0 | 01:14:48.400 | 101.0 | 19:29:01.189 | 1728.383577494984 | 62883779.7 | 36383 |
- | sclk[0] (MHz) | 2084.0 | 22:58:23.259 | 796.0 | 19:56:22.313 | 1914.0741829975539 | 69639761.0 | 36383 |
- +------------------------------------+------------+--------------+-----------+--------------+--------------------+--------------------+-------+
-
- 请按任意键继续. . .
复制代码
作者: Gin_Q 时间: 2020-5-18 19:52
这个帖子是C语言的http://bbs.bathome.net/thread-55089-1-1.html
比c简洁多了,只是慢了2秒左右!
作者: Gin_Q 时间: 2020-5-18 19:52
printf_链接http://bbs.bathome.net/thread-55765-1-1.html
作者: wujunkai 时间: 2020-5-18 22:34
还是不够精美唉
改天自己写一个看看
先收藏
作者: Gin_Q 时间: 2020-5-18 23:04
回复 4# wujunkai
期待!
作者: netdzb 时间: 2020-5-19 07:06
回复 2# Gin_Q
就是那个计算3万行的吗
作者: Gin_Q 时间: 2020-5-19 11:33
回复 6# netdzb
是的
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |