在Python中删除字符串中的引号 [英] Remove quotes from String in Python
问题描述
我有一个Python代码,该代码可以使用Google STT引擎识别语音并将结果返回给我,但结果以带引号的字符串形式出现.我不希望在我的代码中加上引号,因为我将使用它来运行许多命令,但它不起作用.到目前为止,我还没有尝试过任何东西! 这是python代码中的可识别语音的函数:
I have a python Code that will recognize speech using the Google STT engine and give me back the results but I get the results in strings with "quotes". I don't want that quotes in my code as I will use it to run many commands and it doesn't work. I haven't tried anything so far as I didn't get anything to try! This is the function in the python code that will recognize speech:
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:
This is 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程序
This was taken from Steven Hickson's Voicecommand Program made for Raspberry Pi
推荐答案
仅在字符串方法.replace()
始终出现时使用,否则使用.strip()
如果仅在开始和/或结束时出现:
Just use string methods .replace()
if they occur throughout, or .strip()
if they only occur at the start and/or finish:
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中删除字符串中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!