steven96 2018-10-09 01:33 采纳率: 50%
浏览 67690
已采纳

关于python的could not convert string to float的问题。

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import time
import subprocess
import os
from subprocess import call
import csv

#change offset here
offset_peak = 45.0
offset_rsm = 40.0
header_csv = ("time", "amplitude", "rms")

try:
while True:
#pkill because sometimes my microphone was busy
subprocess.call("pkill -9 sox | pkill -9 arecord",shell= True)
time.sleep( 1 )

    #time
    filedate = time.strftime("%Y%m%d-%H%M%S")
    filename = "/home/pi/noise/mp3/" + time.strftime("%Y%m%d") + "/" + filedate + ".mp3"
    filename_csv = "/home/pi/noise/csv/" + time.strftime("%Y%m%d") + ".csv"
    filedate_csv  = time.strftime("%Y-%m-%d %H:%M")
    terminal_time = time.strftime("%H:%M ")

    #record
    subprocess.call("arecord -D hw:1,0 -d 10 -v --fatal-errors --buffer-size=192000 -f dat -t raw --quiet | lame -r --quiet --preset standard - " + filename,shell= True)
    proc = subprocess.getoutput("sox " + filename + " -n stat 2>&1 | grep 'Maximum amplitude' | cut -d ':' -f 2")
    proc_rms = subprocess.getoutput("sox " + filename + " -n stat 2>&1 | grep 'RMS.*amplitude' | cut -d ':' -f 2")
    os.system('clear')
    proc1 = proc.strip()
    proc1 = float(proc1)
    proc_rms = proc_rms.strip()
    proc_rms = float(proc_rms)

    #test your microphone in 5 dB steps and create the function e.g. with mycurvefit.com
    #Fkt 3 30-80 dB
    proc3 = 83.83064 + (28.34183 - 83.83064)/(1 + (proc1/0.04589368)**1.006258)
    #Fkt RMS 30-80 dB
    proc3_rms = 87.69054 + (23.81973 - 87.69054)/(1 + (proc_rms/0.01197014)**0.7397556)

    #add db filextentions: peak - rms
    ext_peak = int(round(proc3, 0))
    ext_rms = int(round(proc3_rms, 0))


    print("Measured values: " + str(proc1) + " / " + str(proc_rms) + " / " + str(proc3) + " / " + str(proc3_rms) + " / " + str(ext_peak) + "\n")

    #csv
    file_exists = os.path.isfile(filename_csv)
    daten_csv = (filedate_csv, proc3, proc3_rms)
    with open(filename_csv, 'a', newline='') as f:
        writer = csv.writer(f)
        if not file_exists:
            writer.writerow(header_csv)
        writer.writerow(daten_csv)

    if proc3 >= offset_peak or proc3_rms >= offset_rsm:
                print(terminal_time + "Sound detected - save: " + filedate + ".mp3 \n")
                os.rename(filename, "/home/pi/noise/mp3/" + time.strftime("%Y%m%d") + "/" + filedate + "-" + str(ext_peak) + "-" + str(ext_rms) + ".mp3")
                time.sleep( 3 )
                #os.system('clear')

    else: 
        print(terminal_time + "No sound detected, delete: " + filedate + ".mp3 \n")
        os.remove(filename)
        time.sleep( 3 )
        #os.system('clear')

except KeyboardInterrupt:
subprocess.call("pkill -9 sox | pkill -9 arecord",shell= True)
print('End')

详细代码如上,当我运行的时候不知为何python shell显示错误,错误信息如下:

Trackback (most recent call last):
File:"/home/pi/noise_level_protocol-master/detect.py, line 30 in
proc1 = float(proc1)
ValueError: could not convert string to float:

希望论坛里的各位大神能够指点迷津,
谢谢。

  • 写回答

1条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2018-10-09 01:49
    关注

    错误说了很清楚了,不能将string类型转化为float ValueError: could not convert string to float。
    排除错误:你先debug调试,你的proc1这个字符串内容事什么,是数字没问题,是其他的就报错。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?