invalid character in identifier error in Python code

17,620

The seems to be the double quotes you used, you used which is different then ". This means that python didn't know it is a string, and this is why you got a syntax error.

This can be a problem with your text editor, i would recommand using sublime text or npp.

And another thing, what you are trying to do probably won't work, since if there's an error retriving the matches, you can't be sure the dict will have the key "data". I would recommand using try-except to know if the response contains any data, or using

d = r.json() if "data" in d: print "Got data" else: print "Error getting data"

Share:
17,620

Related videos on Youtube

Vrajesh Doshi
Author by

Vrajesh Doshi

I am working as Web Developer at Margosa Tree LLP. My key interests and skillset include PHP, javascript, jQuery, AJAX, HTML5, CSS, working with Google maps APIs and Web Push Notifications. I am currently working on PHP frameworks: Codeigniter and Laravel. My domain interests are Software Engineering, Education Technology, Image Processing, Algorithm Analysis. I have published research papers at IEEE conferences and in Peer reviewed international journal, the links to which can be found below. Competitor Driven Development: Hybrid of Extreme Programming and Feature Driven Reuse Development. Realizing Students' Understanding through Rule Based Reasoning. State of the Art Technique for Recognizing Understanding of Learners'

Updated on June 04, 2022

Comments

  • Vrajesh Doshi
    Vrajesh Doshi almost 2 years

    I am getting "invalid character in identifier" error, for the below code. The 'http' in line 3 gets highlighted when the error is shown. I am new to Python, please help.

    import requests
    import html 
    r = requests.get(“http://cricapi.com/api/cricket”)
    if r.status_code == 200:
    currentMatches = r.json()[“data”]
    for match in currentMatches:
    print(html.unescape(match[“title”]))
    else:
    print(“Error in retrieving the current cricket matches”)
    
    • Robin
      Robin over 6 years
      try using normal quote characters? " instead of “
    • Vrajesh Doshi
      Vrajesh Doshi over 6 years
      Thank You, the error got removed but not getting any output
    • Manoj Jadhav
      Manoj Jadhav over 6 years
      @VrajeshDoshi check r.text. you will get to know what API does expect.
    • Vrajesh Doshi
      Vrajesh Doshi over 6 years
      Getting Error: ImportError: No module named html
    • Vrajesh Doshi
      Vrajesh Doshi over 6 years
      Got it, it was expecting apikey
    • hyper-neutrino
      hyper-neutrino over 6 years
      Try indenting your code? This could just be a copy-paste error because there is no way this code won't error.
    • Vrajesh Doshi
      Vrajesh Doshi over 6 years
      Thanks HyperNeutrino, for your valuable input, but it already got resolved some times back.