我是靠谱客的博主 无聊高跟鞋,最近开发中收集的这篇文章主要介绍python中间值怎么获得_Python脚本-创建平方中间值,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#!/usr/bin/env python3fromoptparseimportOptionParserfromsysimportargvimportosfromrandomimportrandintdefread(file_name):b=[]withopen(file_name)asinput_file:forlineininput_file.readlines():parts=line.strip().split(',')b.append({'t':int(parts[0]),'m1':int(parts[1]),'m2':int(parts[2]),'m3':int(parts[3]),'m4':int(parts[4])})returnbdefhandle_args():parser=OptionParser()parser.add_option('-i','--input',dest='input',help='Input config file that will be read from')parser.add_option('-o','--output',dest='output',help='Output config file that will be written to')(opts,args)=parser.parse_args()ifnotopts.input:parser.error('Input file not given. {0} -h for help'.format(argv[0]))parser.print_help()ifnotopts.output:parser.error('Output file not given. {0} -h for help'.format(argv[0]))parser.print_help()returnoptsdefwrite_line(values):"""

write a csv line in output file, out of a values dict

:param values: dict with the data (as declared in read()

:return: None

"""withopen(OUTPUT_FILE_NAME,'a')asout:out.write('RCOU,')out.write(str(values['t']*1000)+',')for_minmotors:out.write(str(round(values[_m]))+',')out.write('0,0,0,0,0,0,0,0,0,0')out.write('n')if__name__=='__main__':options=handle_args()# handle arguments givenkstep_size=100# msmotors=('m1','m2','m3','m4')# the motors. Only as field accessorsOUTPUT_FILE_NAME=options.output# the output file to write data to. (given in the args)INPUT_FILE_NAME=options.input# the output file to read data from. (given in the args)print('reading from',INPUT_FILE_NAME)print('writing to',OUTPUT_FILE_NAME)blocks=read(options.input)# read all lines# delete file if already existsifos.path.exists(OUTPUT_FILE_NAME):os.remove(OUTPUT_FILE_NAME)# if more than one line in fileiflen(blocks)>1:withopen(OUTPUT_FILE_NAME,'a')asout:out.write('samp_rate=44100nchannel=1nsamp_size=16ncodec=Audio/PCMnbyte_order=QAudioFormat::LittleEndiannsample_type=QAudioFormat::SignedIntn')# out.write('channel=1n')# out.write('samp_size=16n')# out.write('codec=Audio/PCMn')# out.write('byte_order=QAudioFormat::LittleEndiann')# out.write('sample_type=QAudioFormat::SignedIntn')# out.write('n')write_line(blocks[0])# write the first line of the input file directly to the output file !!!+1000foriinrange(0,len(blocks)-1):# for every line except the last oneblock=blocks[i]# the current valuesnext_block=blocks[i+1]# the next values# calculate the step based on the current and next time value and the step_sizesteps_count=int((next_block['t']-block['t'])/step_size)motors_steps={}# get data of the motor fieldsforminmotors:# calculate the step of every motor based on the time valuemotors_steps[m]=float((next_block[m]-block[m])/steps_count)forsinrange(1,steps_count+1):data={'t':block['t']+(step_size*s)}# get every interim step for timeforminmotors:# data[m] = randint(1250,1500) # get every step random value for every motordata[m]=(block[m]+(motors_steps[m]*s))*10+1000# get every interim step for every motor# push data to filewrite_line(data)withopen(OUTPUT_FILE_NAME,'a')asout:out.write('RCOU,10100000,56797,56797,56797,56797,0,0,0,0,0,0,0,0,0,0')else:print('not enough data...')

最后

以上就是无聊高跟鞋为你收集整理的python中间值怎么获得_Python脚本-创建平方中间值的全部内容,希望文章能够帮你解决python中间值怎么获得_Python脚本-创建平方中间值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部