在 Python 中删除 String 中的引号

我有一个 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

这是 language-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

这是从史蒂文 · 希克森为树莓派制作的语音指令程序中截取的

359747 次浏览

You can replace "quote" characters with an empty string, like this:

>>> a = '"sajdkasjdsak" "asdasdasds"'
>>> a
'"sajdkasjdsak" "asdasdasds"'
>>> a = a.replace('"', '')
>>> a
'sajdkasjdsak asdasdasds'

In your case, you can do the same for out variable.

if string.startswith('"'):
string = string[1:]


if string.endswith('"'):
string = string[:-1]

There are several ways this can be accomplished.

  • You can make use of the builtin string function .replace() to replace all occurrences of quotes in a given string:

    >>> s = '"abcd" efgh'
    >>> s.replace('"', '')
    'abcd efgh'
    >>>
    
  • You can use the string function .join() and a generator expression to remove all quotes from a given string:

    >>> s = '"abcd" efgh'
    >>> ''.join(c for c in s if c not in '"')
    'abcd efgh'
    >>>
    
  • You can use a regular expression to remove all quotes from given string. This has the added advantage of letting you have control over when and where a quote should be deleted:

    >>> s = '"abcd" efgh'
    >>> import re
    >>> re.sub('"', '', s)
    'abcd efgh'
    >>>
    

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('\"')

You can use eval() for this purpose

>>> url = "'http address'"
>>> eval(url)
'http address'

while eval() poses risk , i think in this context it is safe.

The easiest way is:

s = '"sajdkasjdsaasdasdasds"'
import json
s = json.loads(s)

To add to @Christian's comment:

Replace all single or double quotes in a string:

s = "'asdfa sdfa'"


import re
re.sub("[\"\']", "", s)


This will remove the first and last quotes in your string

import ast


example = '"asdfasdfasdf"'
result = ast.literal_eval(example)


print(result)

Output:

asdfasdfasdf