[Tutor] Need help in understanding why I'm now getting a SyntaxError on a previously working code

Joe Huang yjhuang at whiteboxtechnologies.com
Mon Jun 26 00:58:09 EDT 2023


This is my first post on tutor at python.org. I have 30+ years in scientific programming, but 
it was all in Fortran and awk, never in python which I heard of only towards the end of my 
employment at a national laboratory which I left in 2007.  However, in my private venture 
since I've found it increasingly necessary to use python for tasks such as downloading 
data from the Web by calling an API. Fortunately, my son is very familiar with python and 
had helped me write such a script that was working when I last used it in April. 
Unfortunately, when I tried using it again today, I got this SyntaxError that's got me 
stumped:

   File "wkg_API_points.py", line 33
     filename = f"{locname}_{latitude}_{longitude}_{start}-{end}_CERES.txt"
                                                        ^
SyntaxError: invalid syntax

My suspicion is that I might be using an outdated version, since I dimly remember having 
to update python before I ran the script back in April.  But then I don't understand why 
the python I'm now calling has suddenly gone back to an earlier version, i.e., Python 
2.7.  Since I'm very unfamiliar with python syntax, I thought I'd best ask someone who 
knows python what the problem really and how I can fix it.

The complete code is attached along with a sample input file with 7 locations.  If 
anyone's curious what this code is supposed to do, it retrieves synthetic weather data for 
any location on earth just by giving the latitude, longitude, and time period.

I see two possible options to solve this problem:  (1) if it is a problem due to the 
python version,  I would need to find what happened to my Python 3.X ?  (2) if it's a 
generic syntax error, please tell me what that line should be?

Thanks.

Incidentally, my son is now on travel abroad, so I don't want to bother him with this 
little question.

Joe

-- 
Joe Huang (Huang rhymes with long)
White Box Technologies, Inc.
346 Rheem Blvd., Suite 205A
Moraga CA 94556
yjhuang at whiteboxtechnologies.com
http://weather.whiteboxtechnologies.com for simulation-ready weather data
(o) (925)388-0265
(c) (510)928-2683
-------------- next part --------------
'''
*Version: 2.0 Published: 2021/03/09* Source: [NASA POWER](https://power.larc.nasa.gov/)
POWER API Multi-Point Download
This is an overview of the process to request data from multiple data points from the POWER API.
'''

import os, json, requests

def readFile(filename):
    f = open(filename, "r+")
    lines = f.read().split("\n")
    f.close()

    result = []

    for line in lines:
        if len(line) <= 3:
            break
        else:
            pieces = line.split(" ")
            pieces_abbr = [x for x in pieces if x != '']
            result.append(pieces_abbr)

    return result
    
locations = readFile("wkg_API_inp.txt")
              
output = r""
base_url = r"https://power.larc.nasa.gov/api/temporal/hourly/point?parameters=ALLSKY_SFC_SW_DWN,ALLSKY_SFC_SW_DNI,SZA,ALLSKY_SFC_LW_DWN&community=RE&longitude={longitude}&latitude={latitude}&start=20{start}0101&end=20{end}1231&format=ASCII"

for locname, latitude, longitude, start, end in locations:
    api_request_url = base_url.format(longitude=longitude, latitude=latitude, start=start, end=end)
    filename = f"{locname}_{latitude}_{longitude}_{start}-{end}_CERES.txt"

    response = requests.get(url=api_request_url, verify=True, timeout=30.00)

    f = open(filename,"w+")
    #f = open("test.txt")
    #f.write('\n'.join(response.content.decode('utf-8').split("\n")[25:]))
    f.write(response.content.decode('utf-8'))
    f.flush()
    f.close()
    #print(f"We just finished doing the file {latitude}_{longitude}")
    
    #content = json.loads(response.content.decode('utf-8'))
    #filename = response.headers['content-disposition'].split('filename=')[1]

    #filepath = os.path.join(output, filename)
    #with open(filepath, 'w') as file_object:
        #json.dump(content, file_object)
        
    print("Finished writing to this file: "+filename)
-------------- next part --------------
MS_UNIVERSITY-OXFORD_720541    34.383  -89.550  20  22
QC_MCTAVISH_716120             45.500  -73.567  20  22                
QC_MONTREAL-ST-HUBERT_713710   45.517  -73.417  20  22                
QC_MONTREAL-IAP_716270         45.467  -73.733  20  22                
QC_MONTREAL-IAP-MIRABEL_719050 45.683  -74.033  20  22                
PA_PHILADEPHIA-IAP_724040      39.873  -75.227  21  22
VA_WASHINGTON-REAGAN-AP_724050 38.847  -77.035  21  22


More information about the Tutor mailing list