python | 计算数值文本某一列最小值,将该列原数值加上该最小值

在用dn,ds计算w值时,如果ds(分母)的值为0,那就没法计算了,现在想通过将所有ds值加上大于0的最小值,作为新的ds值,可以通过下面的python脚本实现。

该脚本为一个传参脚本,需要在cmd下运行,格式如下:

python ds.py --input xx.txt --output xx.txt
--input 为输入文件的路径与文件名
--output 为输出文件的路径与文件名

原代码:

import argparse

parser = argparse.ArgumentParser(description="Input and output file path.")
parser.add_argument('--input',type=str,help="The input file path")
parser.add_argument('--output',type=str,help="The output file path")

args=parser.parse_args()

file1=open(args.input,'r')
file2=open(args.output,'w')

# file1 = open("D:\\python_files\\ds\\ds.txt", 'r')
# file2 = open('D:\\python_files\\ds\\ds_xiugai.txt', 'w')
data = []
new_data = []
without_zero_data = []
# Read the value of each line in the file and save it to a list.
for line in file1.readlines():
    data.append(line.strip())
print(data)
# Convert the value in the list from a string to a floating point.
for i in data:
    new_data.append(float(i))
print(new_data)
# Del the zero value in the list,and then use min() to find the min value.
for j in new_data:
    if j != 0:
        without_zero_data.append(j)
min_value = min(without_zero_data)
# print(min_value)

# Add the minimum value to each of the value in the list.
for i, k in enumerate(new_data):
    new_data[i] += min_value
print(new_data)

for z in range(0, len(new_data)):
    file2.write(str(new_data[z]) + "\n")
file1.close()
file2.close()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值