python计算两个数的百分比方法
作者:海边的椰子树 时间:2021-06-20 16:39:45
工作中遇到了要计算两个数百分比的问题,python 2.7 环境。
代码:
#!/usr/bin/env python
#function: 计算百分比
#USAGE: python calculator.py num1 num2
import sys
a=sys.argv[1]
a=float(a)
b=sys.argv[2]
b=float(b)
print "%.2f%%" % (a/b*100)
示例:
root@ops-docker-1:/tmp/data# python calculator.py 3 11
27.27%
下面是我工作中的应用
需求:要计算一堆数的百分比 并记录在文件中
原始文件
-------
root@ops-docker-1:/tmp/data# head -n 7 a.txt
北京 总计:2488 北京 总计:78353
电信 301 电信 6515
移动 70 移动 2349
联通 1500 联通 36736
铁通 60 铁通 1671
教育网 12 教育网 234
鹏博士 9 鹏博士 51
shell脚本
---------
root@ops-docker-1:/tmp/data# cat ratio.sh
#!/bin/bash
#USAGE: sh ratio.sh src-file dst-file
>$2
i=1
for n in $(grep -E -o "([0-9]*)" $1)
do
if [ $(($i%2)) -ne 0 ];then
echo $n >A
else
echo $n >B
A=$(cat A)
B=$(cat B)
echo $A "\t" $B "\t" $(python calculator.py $A $B)>>$2
fi
i=$(($i+1))
done
rm -rf A B
使用
----
root@ops-docker-1:/tmp/data# sh ratio.sh a.txt b.txt
root@ops-docker-1:/tmp/data# head -n 7 b.txt
2488 78353 3.18%
301 6515 4.62%
70 2349 2.98%
1500 36736 4.08%
60 1671 3.59%
12 234 5.13%
9 51 17.65%
合并两个文件格式化处理后显示
root@ops-docker-1:/tmp/data# head -n 7 ab.txt
--------------
北京 总计:2488 北京 总计:78353 2488 78353 3.18%
电信 301 电信 6515 301 6515 4.62%
移动 70 移动 2349 70 2349 2.98%
联通 1500 联通 36736 1500 36736 4.08%
铁通 60 铁通 1671 60 1671 3.59%
教育网 12 教育网 234 12 234 5.13%
鹏博士 9 鹏博士 51 9 51 17.65%
来源:https://blog.csdn.net/Hehailiang_Dream/article/details/54944392
标签:python,百分比
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pandas使用Merge与Join和Concat分别进行合并数据效率对比分析
2023-03-13 12:14:01
![](https://img.aspxhome.com/file/2023/6/90926_0s.png)
4款Python 类型检查工具,你选择哪个呢?
2021-10-29 22:30:27
![](https://img.aspxhome.com/file/2023/4/92424_0s.png)
ASP生成数字相加求和的BMP图片验证码
2011-04-14 10:48:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125111945909s.jpg)
教女朋友学Python3(二)简单的输入输出及内置函数查看 <font color=red>原创</font>
2022-11-14 08:32:18
![](https://img.aspxhome.com/file/2023/6/76026_0s.png)
Django实现简单分页功能的方法详解
2021-02-07 01:54:23
Python使用django获取用户IP地址的方法
2021-07-22 04:01:05
Python 注解方式实现缓存数据详解
2023-07-15 22:11:22
python redis连接 有序集合去重的代码
2023-10-23 21:24:32
python 时间处理之月份加减问题
2023-11-08 01:05:42
![](https://img.aspxhome.com/file/2023/5/63955_0s.png)
Ajax+PHP边学边练 之五 图片处理
2023-11-18 07:21:29
![](https://img.aspxhome.com/file/2023/7/102087_0s.png)
JavaScript运动框架 多值运动(四)
2023-09-08 01:44:51
![](https://img.aspxhome.com/file/2023/5/56155_0s.jpg)
Python 取numpy数组的某几行某几列方法
2023-11-24 05:46:47
![](https://img.aspxhome.com/file/2023/5/64655_0s.png)
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2023-04-21 17:37:43
![](https://img.aspxhome.com/file/2023/9/101159_0s.jpg)
CSS模块化设计—从空格谈起
2007-12-15 09:41:00
python 实现汉诺塔游戏
2021-06-28 10:47:51
![](https://img.aspxhome.com/file/2023/0/95770_0s.png)
python基于pygame实现飞机大作战小游戏
2021-04-27 07:59:17
![](https://img.aspxhome.com/file/2023/8/81528_0s.jpg)
Django2.1.7 查询数据返回json格式的实现
2023-09-28 09:54:20
![](https://img.aspxhome.com/file/2023/9/63239_0s.png)
python开发之list操作实例分析
2023-04-17 16:07:33
python访问sqlserver示例
2022-07-15 07:25:01
解决Jstree 选中父节点时被禁用的子节点也会选中的问题
2023-09-03 06:56:01
![](https://img.aspxhome.com/file/2023/6/56186_0s.png)