1
I am working with data similar to the structure below:
{"Id":1,
"Data_inscricao":"2017-01-01",
"Texto":"Loremipsum",
"Numeracao":26,
"Tempo":"25s"},
{"Id":3,
"Data_inscricao":"2010-05-02",
"Texto":"LoremipsumLorem",
"Numeracao":656,
"Tempo":9},....
I have in hand the die "656" which refers to the "Numeration". I need to get back 2 position of my . get("Numbering") to pick up the die "2010-05-02", ie use the . get("Date Registration") but with reference to "Number":656
How do I do this in JSON or list format variable? Current code below:
numeracao = '656'
#A URL é privada, não vou conseguir mostrar o conteúdo
html = urlopen("https://www.teste.com.br")
#Retornando um volume muito grande de dados, não são apenas 2 blocos de registros.
bsObj = BeautifulSoup(html)
informacoes = bsObj.findAll(id="Resultados")
print(informacoes)
#Resultado do print() - BEGIN
[<input id="Resultados" type="hidden" value="{
"result":true,"message":"ok","Contador":2282,"Dados":
[
{"Id":1,
"Data_inscricao":"2017-01-01",
"Texto":"Loremipsum",
"Numeracao":26,
"Tempo":"25s"},
{"Id":3,
"Data_inscricao":"2010-05-02",
"Texto":"LoremipsumLorem",
"Numeracao":656,
"Tempo":9}
]
}"/>]
#Resultado do print() - END
informacoes = informacoes.replace('"', '\"')
print(type(informacoes))
#Resultado do print() - BEGIN
<class 'str'>
#Resultado do print() - END
print(informacoes)
#Resultado do print() - BEGIN
[<input id="Resultados" type="hidden" value="{
"result":true,"message":"ok","Contador":2282,"Dados":
[
{"Id":1,
"Data_inscricao":"2017-01-01",
"Texto":"Loremipsum",
"Numeracao":26,
"Tempo":"25s"},
{"Id":3,
"Data_inscricao":"2010-05-02",
"Texto":"LoremipsumLorem",
"Numeracao":656,
"Tempo":9}
]
}"/>]
#Resultado do print() - END
regex = re.compile('(?:\"Dados\":\[)(.*?)(?:[]}"/>]])')
informacoes = re.findall(regex, informacoes)
print(type(informacoes))
#Resultado do print() - BEGIN
<class 'list'>
#Resultado do print() - END
#Imprime conteúdo, considerando como lista
for dados in informacoes:
print(type(dados))
#Resultado do print() - BEGIN
<class 'str'>
#Resultado do print() - END
print(dados)
#Resultado do print() - BEGIN
{"Id":1,
"Data_inscricao":"2017-01-01",
"Texto":"Loremipsum",
"Numeracao":26,
"Tempo":"25s"},
{"Id":3,
"Data_inscricao":"2010-05-02",
"Texto":"LoremipsumLorem",
"Numeracao":656,
"Tempo":9
#Resultado do print() - END
#No print acima, realmente está faltando a } no final, provavelmente é por causa da regex
Can you elaborate on the question? Is your JSON an object list? What code are you using so far?
– Woss
@Anderson, I’m not sure, but I believe it’s a list of objects because JSON is being the return of a beautifulsoup. I don’t have a logic for that code yet
– DaniloAlbergardi
Can you put JSON in full then? Don’t forget your code too...
– Woss
@Andersoncarloswoss, I put the code
– DaniloAlbergardi
The question is at least strange, the concept of json is "key:value", so it doesn’t make much sense to express "back 2 positions." If you know the key, go right to it. If you do not know but know the value, convert to a Dict and look for the value to discover the key, the problem is if the same value belongs to more than one key.
– Sidon
@Sidon , Yes, a value can belong to more than one key with the same name. In the case of the expression "back 2 positions", it is based on I own the value 656 and need to collect the value of the key number, only from the data block with id:3. Obs. I don’t have id value
– DaniloAlbergardi
Without the real file in hand it becomes difficult.
– Sidon
I changed the question to make it clearer, I could analyze it please?
– DaniloAlbergardi
@Daniloalbergardi Okay, I put an answer, see if it helps.
– Sidon