在Python中删除String中的引号
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Python中删除String中的引号,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1833字,纯文字阅读大概需要3分钟。
内容图文
![在Python中删除String中的引号](/upload/InfoBanner/zyjiaocheng/710/b6c64e04dddc438cb5881d9349f05a54.jpg)
我有一个python代码,可以使用谷歌STT引擎识别语音并给我回复结果,但我得到的结果是带有“引号”的字符串.我不想在我的代码中使用引号,因为我将使用它来运行许多命令,但它不起作用.到目前为止,我没有尝试任何东西,因为我没有尝试任何东西!
这是python代码中将识别语音的函数:
def recog():
p = subprocess.Popen(['./speech-recog.sh'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
global out,err
out, err = p.communicate()
print out
这是speech-recog.sh:
#!/bin/bash
hardware="plughw:1,0"
duration="3"
lang="en"
hw_bool=0
dur_bool=0
lang_bool=0
for var in "$@"
do
if [ "$var" == "-D" ] ; then
hw_bool=1
elif [ "$var" == "-d" ] ; then
dur_bool=1
elif [ "$var" == "-l" ] ; then
lang_bool=1
elif [ $hw_bool == 1 ] ; then
hw_bool=0
hardware="$var"
elif [ $dur_bool == 1 ] ; then
dur_bool=0
duration="$var"
elif [ $lang_bool == 1 ] ; then
lang_bool=0
lang="$var"
else
echo "Invalid option, valid options are -D for hardware and -d for duration"
fi
done
arecord -D $hardware -f S16_LE -t wav -d $duration -r 16000 | flac - -f --best --sample-rate 16000 -o /dev/shm/out.flac 1>/dev/shm/voice.log 2>/dev/shm/voice.log; curl -X POST --data-binary @/dev/shm/out.flac --user-agent 'Mozilla/5.0' --header 'Content-Type: audio/x-flac; rate=16000;' "https://www.google.com/speech-api/v2/recognize?output=json&lang=$lang&key=key&client=Mozilla/5.0" | sed -e 's/[{}]/''/g' | awk -F":" '{print $4}' | awk -F"," '{print $1}' | tr -d '\n'
rm /dev/shm/out.flac
这取自Steven Hickson为Raspberry Pi制作的Voicecommand Program
解决方法:
如果它们全部发生,只使用字符串方法.replace(),如果它们仅在开始和/或结束时出现,则使用.strip():
a = '"sajdkasjdsak" "asdasdasds"'
a = a.replace('"', '')
'sajdkasjdsak asdasdasds'
# or, if they only occur at start and end...
a = a.strip('\"')
'sajdkasjdsak" "asdasdasds'
# or, if they only occur at start...
a = a.lstrip('\"')
# or, if they only occur at end...
a = a.rstrip('\"')
内容总结
以上是互联网集市为您收集整理的在Python中删除String中的引号全部内容,希望文章能够帮你解决在Python中删除String中的引号所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。