Re: Python discard extra information

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 11 Feb 2010 16:43:56 -0800

Hi Storm and Jim,

Storm, your method may not always work as you rely on character positions for a 
specific string.

IN Python 2.6 the json library is available via:
import json

Then change Jim's code to use json.loads and everything will work as Jim showed 
it.

Richard
  ----- Original Message ----- 
  From: Storm Dragon 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Thursday, February 11, 2010 7:51 AM
  Subject: Re: Python discard extra information


  Hi,
  I tested the method you gave me and unfortunately I didn't have the 
django.utils library installed by default. Your solution did help me though, 
because for some reasn the word string stuck out at me this time. Why it didn't 
before, why I refused to treat the result like a string in the first place, I 
will never know. But finally, your message made it click. So, I treated it like 
a string, a string in which the beginning and the end, assuming all goes well, 
are the same. Now, I have what I needed, the result without having to install 
non-default libraries, at least I hope anyway. Here's the program in it's 
working state:
  import urllib
  text = "hello, how are you today?"
  languageCode = 'es'
  newText = 
urllib.urlopen("//ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=";
 + urllib.quote(text) + "&langpair=en|" + urllib.quote(languageCode)).read()
  newText = newText[36:-51]
  print newText

  thanks so much everyone for all the help
  Storm


-- 
Follow me on Twitter:
http://www.twitter.com/stormdragon2976
My blog, Thoughts of a Dragon:
http://www.stormdragon.us/
What color dragon are you?
http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/


       
  On Thu, 2010-02-11 at 12:33 +0000, Jim Dunleavy wrote: 
    Hi Storm, 
     
    The result is json. 
    Use a json library to make a Python structure out of the string. 
    I think recent versions of Python (2.6?) have a json module as standard. 
    But the following works if you have the simplejson from Django: 
     
    from django.utils import simplejson 
     
    result = simplejson.loads(NewText) 
    print result['responseData']['translatedText'] 
     
    --Jim 
     
    ----- Original Message ----- 
    From: Storm Dragon 
    To: programmingblind@xxxxxxxxxxxxx 
    Sent: Thursday, February 11, 2010 2:09 AM 
    Subject: Re: Python discard extra information 



    Hi,
    Here's the output:
    {"responseData": {"translatedText":"Hola, ¿cómo estás hoy?"}, 
"responseDetails": null, "responseStatus": 200}
    Here's the part I need in this version of the program, if the text variable 
changes, so does the required output:
    Hola, ¿cómo estás hoy?
    thanks
    Storm


-- 
Follow me on Twitter:
http://www.twitter.com/stormdragon2976
My blog, Thoughts of a Dragon:
http://www.stormdragon.us/
What color dragon are you?
http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/



         
    On Wed, 2010-02-10 at 17:50 -0800, R Dinger wrote:

       
      What other stuff are you getting? 
      What exactly does your program output in the print statement? 
      Richard 

        ----- Original Message ----- 
        From: Storm Dragon 
        To: programmingblind@xxxxxxxxxxxxx 
        Sent: Wednesday, February 10, 2010 4:55 PM 
        Subject: Python discard extra information 


        Hi,
        I have written a small program to translate from english to another 
language, in this instance, Spanish. The problem is, it returns some other 
stuff that I don't need. How can I get just the translated text witout the 
other stuff? I only want to use standard libraries and keep it as short as 
possible. Thanks for the help. Here's the code:
        import urllib
        text = "hello, how are you today?"
        languageCode = 'es'
        newText = 
urllib.urlopen("//ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=";
 + urllib.quote(text) + "&langpair=en|" + urllib.quote(languageCode)).read()
        print newText

        Thanks
        Storm


-- 
Follow me on Twitter:
http://www.twitter.com/stormdragon2976
My blog, Thoughts of a Dragon:
http://www.stormdragon.us/
What color dragon are you?
http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/




             

Other related posts: