Pandasdmx fails because DataflowDefinition contains underscore

127 views Asked by At

I am trying to use Python to extract data from a sdmx file using the walkthrough [example][1]

The code below should do the trick, but the DataflowDefinition is a series of digits and results in a syntax error. I assumed it could have been the underscore.

I read the question ["Escape underscore characters in string containing URLs for markdown"][2], and tried to escape the underscore. But it did not work out.

File "C:\Users\...\Extract data ISTAT\ISTAT_data.py", line 30
    crops_flow = crops_msg.dataflow.101_1015
                                   ^
SyntaxError: invalid syntax

I think it is very trivial, but I fail to fix it.

See the code attached below.


istat = sdmx.Request('ISTAT')

flow_msg = istat.dataflow() #We use sdmx to download the definitions for all data flows available from our chosen source. 

print(flow_msg.response.url) # see the URL that was queried 

print(flow_msg.response.headers) # see the response headers

print(flow_msg) #All the content of the response—SDMX data and metadata objects—has been parsed and is accessible from flow_msg. Let’s find out what we have received

dataflows = sdmx.to_pandas(flow_msg.dataflow) # converting metadata to pandas.Series

print(dataflows.head())

print("The file contains", len(dataflows), "data flow definitions")

crops_msg = istat.dataflow("101_1015") # download the data flow definition with the ID '101_1015'

print(crops_msg) # The response includes several classes of SDMX objects. 

crops_flow = crops_msg.dataflow.101_1015 #here the code fails for synthax error 

dsd = crops_flow.structure 
print(dsd) # Show the data structure definition referred to by the data flow

istat = sdmx.Client("ISTAT", backend="memory")

data_msg = istat.data("101_1015", key=key, params=params)

data_msg.response.headers["content-type"] #generic data was returned

# Number of bytes in the cached response

bytes1 = sys.getsizeof(ecb.session.cache.responses.popitem()[1]._content)

print(bytes1) ```


  [1]: https://sdmx1.readthedocs.io/en/latest/walkthrough.html
  [2]: https://stackoverflow.com/questions/58986249/escape-underscore-characters-in-string-containing-urls-for-markdown
1

There are 1 answers

0
Dr. Leo On BEST ANSWER

Use dict syntax instead:

crops_flow = crops_msg.dataflow['101_1015']