Remove quotes from string 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

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;' "//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

This was taken from Steven Hickson's Voicecommand Program made for Raspberry Pi

asked Dec 3, 2016 at 17:55

Alok NaushadAlok Naushad

1,2252 gold badges8 silver badges9 bronze badges

2

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['\"']

answered Dec 3, 2016 at 18:16

smcismci

30.4k18 gold badges110 silver badges145 bronze badges

3

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.

answered Mar 13, 2018 at 4:27

koliyat9811koliyat9811

7791 gold badge7 silver badges11 bronze badges

4

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'
    >>> 
    

answered Dec 3, 2016 at 18:12

Christian DeanChristian Dean

21.3k7 gold badges50 silver badges79 bronze badges

The easiest way is:

s = '"sajdkasjdsaasdasdasds"' 
import json
s = json.loads[s]

answered Sep 1, 2020 at 8:31

RyanRyan

7741 gold badge12 silver badges26 bronze badges

3

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

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

answered Dec 3, 2016 at 18:06

Harald NordgrenHarald Nordgren

11k6 gold badges39 silver badges62 bronze badges

4

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.

answered Dec 3, 2016 at 18:05

Aza TAza T

5794 silver badges11 bronze badges

To add to @Christian's comment:

Replace all single or double quotes in a string:

s = "'asdfa sdfa'"

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

answered Dec 7, 2020 at 15:35

This will remove the first and last quotes in your string

import ast

example = '"asdfasdfasdf"'
result = ast.literal_eval[example]

print[result]

Output:

asdfasdfasdf

answered Oct 8, 2021 at 0:53

How do you remove a quote from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll["^\"|\"$", ""]; After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you remove quotes from a list in Python?

To remove the quotes from a list of strings:.
Use a list comprehension to iterate over the list..
Use the str. strip[] method to remove the leading and trailing quotes from each string..
The items in the new list won't contain leading and trailing quotes..

How do you print a string without quotes in Python?

Ways to print Strings in Python without quotes.
Using sep[] method To Print Without Quotes In Python. Code – ini_list = [ 'a' , 'b' , 'c' , 'd' ] ... .
Using .format[] method To Print Without Quotes In Python. Code – ini_list = [ 'a' , 'b' , 'c' , 'd' ] ... .
Using translate method To Print Without Quotes In Python. Input –.

How do you remove double quotes from a list in Python?

Use the str. strip[] method to remove the leading and trailing double quotes from each string. The items in the new list won't contain leading and trailing double quotes.

Chủ Đề