在用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条)