I am trying to create a dict like this:
{
"Name": "string",
"Info": [
{
"MainID": "00000000-0000-0000-0000-000000000000",
"Values": [
{
"IndvidualID": "00000000-0000-0000-0000-000000000000",
"Result": "string"
},
{
"IndvidualID": "00000000-0000-0000-0000-000000000000",
"Result": "string"
}
]
}
]
}
Where the Values section has 100+ things inside of it. I put 2 as an example.
Unsure how to build this dynamically. Code I have attempted so far:
count = 0
with open('myTextFile.txt') as f:
line = f.readline()
line = line.rstrip()
myValues[count]["IndvidualID"] = count
myValues[count]["Result"] = line
count = count +1
data = {"Name": "Demo",
"Info": [{
"MainID":TEST_SUITE_ID,
"Values":myValues
}}
This does not work however. Due to "Key Error 0" it says. Works if I do it like myValues[count]= count but as soon as I add the extra layer it breaks myValues[count]["IndvidualID"] = count. I see some example of setting a list in there, but I need like a List (Values) with multiple things inside (ID and Result). Have tried a few things with no luck. Anyone have a simple way to do this in python?
Full traceback:
Traceback (most recent call last):
File "testExtractor.py", line 28, in <module>
myValues[count]["IndvidualID"] = count
KeyError: 0
If I add a few bits and pieces I can get this code to run:
count = 0
myValues = []
with open('myTextFile.txt') as f:
for line in f:
line = line.rstrip()
d = {"IndvidualID":count, "Result":line}
myValues.append(d)
count = count + 1
TEST_SUITE_ID = 1
data = {"Name": "Demo",
"Info": [{
"MainID":TEST_SUITE_ID,
"Values":myValues
}]}
Output:
{'Name': 'Demo', 'Info': [{'MainID': 1,
'Values': [{'IndvidualID': 0, 'Result': 'apples'}, {'IndvidualID': 1, 'Result': 'another apples'}]
}]}
Note:
I have defined myValues as an empty list. I iterate over the file to read all lines and I create a new dict for each line, appending to myValues each time. Finally I create the whole data object with myValues embedded inside.
To get the above output I actually substituted: f = ['apples','another apples'] for the open file, but I'm sure an actual file would work.
Related
I have this json file:
[
{
"#timestamp": "",
"userID": "",
"destinationUserName": "",
"message": ": 12,050",
"name": "Purge Client Events"
},
{
"#timestamp": "",
"userID": "",
"destinationUserName": "",
"message": "",
"name": ""
},
{
"#timestamp": "",
"userID": "",
"destinationUserName": "",
"message": "",
"name": ""
},
{
"#timestamp": "",
"userID": "",
"name": "",
"sourceUserName": "",
"deviceAction": ""
}
]
I am looking for a solution in which I can loop over all the file, and count the unique values for UserID and return that value printed.
I found different solution but non of them worked for me and I am completely stuck.
So far this is my code, its just a formatter that convert the file into a json format.
After that I tried to check the length of the file and loop over it appending unique elements.
import json
to_queue = []
def structure_json():
with open("file.json", "r+") as f:
old = f.read()
f.seek(0) # rewind
# save to the old string after replace
new = old.replace('}{', '},{')
f.write(new)
tmps = '[' + str(new) + ']'
json_string = json.loads(tmps)
for key in json_string:
to_queue.append(key)
f.close
with open('update_2.json', 'w') as file:
json.dump(json_string, file, indent=2)
size=len(file["UserID"])
uniqueNames = [];
for i in range(0,size,1):
if(file["UserID"] not in uniqueNames):
uniqueNames.append(file["UserID"]);
print(uniqueNames)
structure_json()
print(to_queue)
But I get the following error:
Traceback (most recent call last):
File "format.py", line 24, in <module>
structure_json()
File "format.py", line 17, in structure_json
size=len(file["UserID"])
TypeError: '_io.TextIOWrapper' object is not subscriptable
Please any help will be much appreciated. Thank you so much for any help, and if you need any more info just let me know
Open the file and load the content. Then you can iterate over list of dicts and crate set of all values for key userID. Note, if any missing key it will yield None and will affect the count (+1).
import json
with open('your_file.json') as f:
data = json.load(f)
users = set(item.get('userID') for item in data)
print(len(users))
print(users)
I have a .txt file like this image for txt file, where every column can be classified as UserName:ReferenceToThePassowrd:UserID:GroupID:UserIDInfo:HomeDir:LoginShell.
I want to make a script to parse the file and generate a list of dictionaries where each user information is represented by a single dictionary:
Here is an example of how the final output should look like:
[
{
"user_name": "user1",
"user_id": "1001",
"home_dir": "/home/user1",
"login_shell": "/bin/bash"
},
{
"user_name": "user2",
"user_id": "1002",
"home_dir": "/home/user2",
"login_shell": "/bin/bash"
}
]
A very basic way of doing this might look like:
objects = []
for line in open("myData.txt"):
line = line.strip()
items = line.split(":")
someDict = {}
someDict["first"] = items[0]
someDict["second"] = items[1]
# ... etc
objects.append(someDict)
print(objects)
Trying to run code where I create empty list by iterating events from json and append userid of any user who did homepage event into the empty list. I'm trying to use conditional parsing to only parse out 'name' key value with pair 'Visited home page' from the events.json
My code is as follows:
import json
#step 1 -- read events file into string
with open('events.json') as events_data:
#step 2 -- convert string to json object (json.load(string)
events_data = json.load(events_data)
#print (events_data['events'][0]['name'])
#print(events_data.keys())
#with open ("data/events.json") as events_file:
#step 3 -- create empty list; iterate events list and append userid of any user who did homepage event into the empty list
#that gives us set of user ids who did homepage event
homeuserid = []
for i in events_data['events']:
homeuserid[i] = i ['name'] ['Visited home page']
print(homeuserid)
However when I go the run the code I get the following error and I am unsure why:
Traceback (most recent call last):
File "run.py", line 34, in <module>
homeuserid[i] = i ['name'] ['Visited home page']
TypeError: string indices must be integers
Sample of JSON (events.json):
{
"events": [
{
"name": "Added item to cart",
"timestamp": 1422119921,
"user_id": "5a5598f2-f7db-420e-9b8e-52a9ad694bc1"
},
{
"name": "Visited product page",
"timestamp": 1409554014,
"user_id": "4683c9b6-3c8b-4215-a401-a9bbfde833ee"
},
{
"name": "Visited about page",
"timestamp": 1430938313,
"user_id": "26a1593b-b17d-4389-aa93-4a1c9c0e9c67"
},
{
"name": "Added item to cart",
"timestamp": 1427447392,
"user_id": "e71f2ee8-09ce-412b-92e1-3c6c0a90dda8"
},
Here's the proper way to do it, there's no reason to process the file twice. Note that the resulting list may be empty if there are no 'Visited home page' events.
import json
filepath = 'events.json'
with open(filepath) as events_file:
events_data = json.load(events_file)
homeuserids = []
for event in events_data['events']:
if event['name'] == 'Visited home page':
homeuserids.append(event['user_id'])
print(homeuserids)
New in JSON/Python... I'd like to select a part of sub category.
Here is a part of the JSON file:
{
"items": [{
"seasonId": 59,
"createdDate": "20200721T205735.000Z",
"participants": [{
"tag": "#8CJ89RJ",
"name": "Cåmille",
"cardsEarned": 1401,
"battlesPlayed": 1,
"wins": 1,
"collectionDayBattlesPlayed": 3,
"numberOfBattles": 1
}, {
"tag": "#Y2828CQ",
"name": "<c2>MoutBrout",
"cardsEarned": 1869,
"battlesPlayed": 1,
"wins": 1,
"collectionDayBattlesPlayed": 3,
"numberOfBattles": 1
}, {
"tag": "#2Q8CRC8RY",
"name": "Desnoss",
"cardsEarned": 2337,
"battlesPlayed": 1,
"wins": 0,
"collectionDayBattlesPlayed": 3,
"numberOfBattles": 1
}, {
"tag": "#80CGRR2CY",
"name": "pixtango",
"cardsEarned": 1402,
"battlesPlayed": 1,
"wins": 1,
"collectionDayBattlesPlayed": 2,
"numberOfBattles": 1
}]
}]
}
I would like a result as:
Camille - 1401 cards - 1 win
etc
However, my issue is that those infos are under items/0/participants.
I know how to do with data under one category. Here is an exemple for another JSON file and how I'd like the new one to be:
for item in data ["items"][:5]:
print("Name: %s\nTrophies: %s\nTag: %s\n\n" % (
item["name"],
item["trophies"],
item["tag"],
))
Any idea please ?
EDIT: I'm sorry, here is how it looks:
For exemple, I would like to print the 5 first names. I put this:
for item in data ["items"][:5]:
print (data[items][0][participants]['name'])
And I received this error:
NameError: name 'items' is not defined
maybe you need something like this:
items_str = [f'Name: {i["name"]}\nTrophies: {i["trophies"]}\nTag: "{i["tag"]}'
for i in json_dict['items']]
for i in items_str:
print(i)
sorry, it's not so easy to understand from your data
UPD: If there are many 'items' with 'participants' in each, this code should work for you:
participants = []
for item in json_dict['items']:
for participant in item['participants']:
p = 'Name: {}\nTrophies: {}\nTag: {}'.format(item["name"], item["trophies"], item["tag"])
participants.append(p)
print(p)
sub_dict = dict['items'][0]['participants']
print("Name: {}\nTrophies: {}\nTag:{}\n\n".format(sub_dict['name'],sub_dict['trophies'],sub_dict['tag']))
For other participants increase the array index.
Reading the JSON data from the file and prints it.
import json
with open('j.json','r') as f:
data = json.loads(f.read())
for item in data['items']:
for p in item['participants']:
print p
print("Name: %s\nTrophies: %s\nTag: %s\n\n" % (
p["name"],
p["trophies"],
p["tag"]))
I think it may help you:
Code:
import json
import pandas as pd
j='{"items":[{"seasonId":59,"createdDate":"20200721T205735.000Z","participants":[{"tag":"#8CJ89RJ","name":"Cåmille","cardsEarned":1401,"battlesPlayed":1,"wins":1,"collectionDayBattlesPlayed":3,"numberOfBattles":1},{"tag":"#Y2828CQ","name":"<c2>MoutBrout","cardsEarned":1869,"battlesPlayed":1,"wins":1,"collectionDayBattlesPlayed":3,"numberOfBattles":1},{"tag":"#2Q8CRC8RY","name":"Desnoss","cardsEarned":2337,"battlesPlayed":1,"wins":0,"collectionDayBattlesPlayed":3,"numberOfBattles":1},{"tag":"#80CGRR2CY","name":"pixtango","cardsEarned":1402,"battlesPlayed":1,"wins":1,"collectionDayBattlesPlayed":2,"numberOfBattles":1}]}]}'
y = json.loads(j)
y=pd.DataFrame([x for x in y['items'][0]['participants']])
print(y)
output:
I am trying to access weather api data. It returns a long a less human readable single line. I am trying to replace every bracket({) with '{/n' so that bracket remains but as well a new line character just for better readable json.
But it returns every character on a new line in the shell.
import urllib2
url2 = 'http://api.openweathermap.org/data/2.5/find?q=london,PK&units=metric'
data = urllib2.urlopen(url2)
s = data.read()
count = 0
s = s.replace('{',"{\n")
#s = ''.join(s)
for line in s:
print line
count = count + 1
print count
after join() the problem still persists.
The problematic output after this code is like this
Why don't you use the built-in capabilities of the json library that's standard in Python?
import urllib2
import json
url2 = 'http://api.openweathermap.org/data/2.5/find?q=london,PK&units=metric'
data = urllib2.urlopen(url2)
# read the contents in and parse the JSON.
jsonData = json.loads(data.read())
# print it out nicely formatted:
print json.dumps(jsonData, sort_keys=True, indent=4, separators=(',', ': '))
output:
{
"cod": "200",
"count": 1,
"list": [
{
"clouds": {
"all": 20
},
"coord": {
"lat": 38.7994,
"lon": -89.9603
},
"dt": 1442072098,
"id": 4237717,
"main": {
"humidity": 67,
"pressure": 1020,
"temp": 16.82,
"temp_max": 18.89,
"temp_min": 15
},
"name": "Edwardsville",
"sys": {
"country": "United States of America"
},
"weather": [
{
"description": "few clouds",
"icon": "02d",
"id": 801,
"main": "Clouds"
}
],
"wind": {
"deg": 350,
"speed": 4.6
}
}
],
"message": "accurate"
}
The issue is here:
for line in s:
print line
At this point, it will print every character on a separate line - that's what print does (it adds a trailing newline to each print command), as shown by this code:
print 1
print
print 2
which outputs this:
1
2
You may be confused with the name line, but it's not a special variable name. You can change the word line to any valid variable name and it will work the same way.
A for loop will iterate over an iterable. If it's a file, it will do each line. A list will be each element, and a string goes over every character. Because you say to print it, it then prints them individually.
Are you expecting a non-string response from the API? If it gives a list like this:
["calls=10","message=hello"]
then your for loop will print each in turn. But if it's just a string, like "message=hello" it will print each character.
And the reason there is a blank newline after the {? Because the replace command is working fine.
s is just a string, so doing for x in s actually iterates over individual characters of s, not over its lines. I think you're confusing it with for line in f when f is a file object!