Pandasdmx失败,因为DataflowDefinition包含下划线



我正在尝试使用Python从使用演练[示例][1]的sdmx文件中提取数据

下面的代码应该可以解决这个问题,但是DataflowDefinition是一系列数字,会导致语法错误。我假设它可能是下划线

我阅读问题["在包含url的字符串中转义下划线字符为markdown"][2],并试图转义下划线。但是没有成功。

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

我认为这是非常微不足道的,但我没有解决它。

请参阅下面的代码。


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

使用字典语法代替:

crops_flow = crops_msg.dataflow['101_1015']

最新更新