栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589337.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号