读取保存数据为数组的txt文件使用try进行异常发现使用while检测文件末尾进行读取1234567891011121314file_to_readraw_input(Enter file name of tests (empty string to end program):)try:infileopen(file_to_read,r)whilefile_to_read ! :file_to_writeraw_input(Enter output file name (.csv will be appended to it):)file_to_writefile_to_write.csvoutfileopen(file_to_write,w)readings(infile.readline())printreadingswhilereadings !0:globalcountreadingsint(readings)minimum(infile.readline())maximum(infile.readline())使用for遍历读取的每一行进行一次性的读取和输入下面调用的程序读取的数据是1234567891011121314result list()withopen(../test/parameter.txt) as f:forlineinf.readlines():temp list()# 逐个遍历对应每一行元素将之转为对应的数据b line.strip(,][).split(,)if(len(b) 5):b.pop()forainb:a a.replace([,).replace(],)temp.append(float(a))result.append(temp)#print(中途打印的temp是,temp)#print(加入到result中的结果是,result)删除str中的特定字符删除字符串首尾的多余字符串strip()1234567# 删除字符串中多余字符defstring_remove():str1 abc \nprintstr1.strip()# abcstr2----abcdfprintstr2.strip(-)# abcdfreplace函数删除字符串中某一个所有的字符串123ssold old stringretss.replace(old,new,1)print(ret)sub函数同时删除多个字符串这里使用了正则表达式123str2\nabc\nwrt22\t666\t# 删除字符串中的所有\n,\timportreprint(re.sub([\n\t],,str2))# abcwrt22666以上就是聊一聊python常用的编程模块的详细内容