I have three lists like below and I want to create a JSON file from them:
devices = ['iphone', 'ipad', 'ipod', 'watch'],
cities = ['NY', 'SFO', 'LA', 'NJ'],
companies = ['Apple', 'Samsung', 'Walmart']
I have done like below.
First manually create a dictionary:
data = {
'devices': ['iphone', 'ipad', 'ipod', 'watch'],
'cities': ['NY', 'SFO', 'LA', 'NJ'],
'companies': ['Apple', 'Samsung', 'Walmart']
}
Then convert it to JSON format like this:
import json
with open('abc.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
Is there a better way of doing this when we have more number of lists.
Ideally if I have N number of lists, I want to create a JSON formatted file a minimal amount of manual work.
Your question doesn't show getting the lists from an external source like another .py file, so here's how to do it given their variable names when they've been defined in-line as shown in it:
import json
devices = ['iphone', 'ipad', 'ipod', 'watch']
cities = ['NY', 'SFO', 'LA', 'NJ']
companies = ['Apple', 'Samsung', 'Walmart']
lists = ['devices', 'cities', 'companies']
data = {listname: globals()[listname] for listname in lists}
with open('abc.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
Contents of the abc.json file it creates:
{
"devices": [
"iphone",
"ipad",
"ipod",
"watch"
],
"cities": [
"NY",
"SFO",
"LA",
"NJ"
],
"companies": [
"Apple",
"Samsung",
"Walmart"
]
}
This method will work for any number of lists providing they have the same format as the ones provided in your question. Hope this helps.
# define the list vars
devices = ['iphone', 'ipad', 'ipod', 'watch'],
cities = ['NY', 'SFO', 'LA', 'NJ'],
companies = ['Apple', 'Samsung', 'Walmart']
# get the variables into a big list
v = locals()['In'][2]
output = {}
#break down the lists and turn them into dict entries
v1 = v.split(',\n')
for each in v1:
#print(each)
name = each.split(' = ')[0]
data = each.split(' = ')[1]
data = data[2:-2]
datalist = data.split("', '")
output[name] = datalist
#show the output
output
#export as JSON
import json
with open('C:\\abc.json', 'w') as outfile:
json.dump(output, outfile, indent=4)
Related
I'm trying to convert an array with a dictionary to a flattened dictionary and export it to a JSON file. I have an initial tab-delimited file, and have tried multiple ways but not coming to the final result. If there is more than one row present then save these as arrays in the dictionary
Name file code file_location
TESTLIB1 443 123 location1
TESTLIB2 444 124 location2
Current Output:
{'library': 'TESTLIB2', 'file': '444', 'code': '124', 'file_location': 'location2'}
Desired Output if num_lines > 1:
{'library': ['TEST1', 'TEST2'], 'file': ['443', '444'], 'code': ['123', 123], 'file_location': ['location1', 'location2]}
Code Snippet
data_dict = {}
with open('file.tmp') as input:
reader = csv.DictReader(input, delimiter='\t')
num_lines = sum(1 for line in open('write_object.tmp'))
for row in reader:
data_dict.update(row)
if num_lines > 1:
data_dict.update(row)
with open('output.json', 'w') as output:
output.write(json.dumps(data_dict))
print(data_dict)
create list for each column and iterate to append row by row
import csv
import json
# read file
d = {}
with open('write_object.tmp') as f:
reader = csv.reader(f, delimiter='\t')
headers = next(reader)
for head in headers:
d[head] = []
for row in reader:
for i, head in enumerate(headers):
d[head].append(row[i])
# save as json file
with open('output.json', 'w') as f:
json.dump(d, f)
output:
{'Name': ['TESTLIB1', 'TESTLIB2'],
'file': ['443', '444'],
'code': ['123', '124'],
'file_location': ['location1', 'location2']}
from collections import defaultdict
data_dict = defaultdict(list)
with open('input-file') as inp:
for row in csv.DictReader(inp, delimiter='\t'):
for key, val in row.items():
data_dict[key].append(val)
print(data_dict)
# output
{'Name': ['TESTLIB1', 'TESTLIB2'],
'file': ['443', '444'],
'code': ['123', '124'],
'file_location': ['location1', 'location2']}
I read a string containing a json document.
d2 = json.loads(s1)
I am getting data in this format, a list of dictionnaries.
[{'creati_id': 123,
'creativ_id': 234,
'status': 'adsc',
'name': 'seded',
…
'video_75_views': None,
'video_100_views': None,
'estimated': None,
'creative1': 1.0,
'creative': 'Excellent',
'value': 1.023424324}]}
How can I save this data in CSV format?
This can easily be achieved with the csv module:
import csv
data = [
{
"creati_id": 123,
"creativ_id": 234,
"status": "adsc",
"name": "seded",
}
]
with open("data_file.csv", "w") as data_file:
csv_writer = csv.writer(data_file)
header = data[0].keys()
csv_writer.writerow(header)
for line in data:
csv_writer.writerow(line.values())
You can use the standard csv library in Python to write CSV files. From your question, I'm assuming that you have multiple rows, each having the structure you shared. If that's the case, then something like this should do the trick:
import csv
json1 = [
{'creati_id': 123, 'creativ_id': 234, 'status': 'adsc', 'name': 'seded', 'email': None, 'brand': 'adc', 'market': 'dcassca', 'channel': 'dAD'},
{'creati_id': 123, 'creativ_id': 234, 'status': 'adsc', 'name': 'seded', 'email': None, 'brand': 'adc', 'market': 'dcassca', 'channel': 'dAD'}
]
header_names = json1[0].keys() # Extract the header names
data_rows = [row.values() for row in json1] # Extract the values for each
with open('output.csv', 'w', encoding='UTF8', newline='') as file:
writer = csv.writer(file)
writer.writerow(header_names) # Writes the header
writer.writerows(data_rows) # Writes the rows
Input:
- A text file that contains 3 lines:
"Thank you
binhnguyen
2010-09-12
I want to say thank you to all of you."
Output:
I want to create a dictionary with fixed keys: 'title', 'name', 'date', 'feedback' that stores 4 lines in the file above respectively.
{'title': 'Thank you', 'name': 'binhnguyen', 'date': '2010-09-12
', 'feedback': 'I want to say thank you to all of you.'}
Thank you so much
You can basically define a list of keys and match them with lines.
Example:
key_list = ["title","name","date","feedback"]
text = [line.replace("\n","").replace("\"","") for line in open("text.txt","r").readlines()]
dictionary = {}
for index in range(len(text)):
dictionary[key_list[index]] = text[index]
print(dictionary)
Output:
{'title': 'Thank you', 'name': 'binhnguyen', 'date': '2010-09-12', 'feedback': 'I want to say thank you to all of you.'}
Given file.txt where the file is and the format is the one described on the question this would be the code:
path = r"./file.txt"
content = open(path, "r").read().replace("\"", "")
lines = content.split("\n")
dict_ = {
'title': lines[0],
'name': lines[1],
'date': lines[2],
'feedback': lines[3]
}
print(dict_)
my csv file is below
Uid,locate,category,act
Anna,NY,house,dance
Anna,LA,house,sing
Anna,CH,house,ride
John,NY,house,dance
John,LA,home,sing
John,CH,home,ride
and i want to create dictionary just like
{'Uid': 'Anna', 'infos':[{'locate': 'NY', 'category': 'house', 'act': 'dance'},
{'locate': 'LA', 'category': 'house', 'act': 'sing'},
{'locate': 'CH', 'category': 'house', 'act': 'ride'}]
},
{'Uid': 'John', 'infos':[{'locate': 'NY', 'category': 'house', 'act': 'dance'},
{'locate': 'LA', 'category': 'home', 'act': 'sing'},
{'locate': 'CH', 'category': 'home', 'act': 'ride'}]
},
my code is below:
result = {}
with open('test.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
result['test_uid'] = row['Uid']
result["test_locate"] = row['locate']
result["test_category"] = row['category']
result["test_act"] = row['act']
print(result)
how to append the infos datas to the same person?
how to fix my code that can print the result I want??
Need someone help please.
Please try the following:
payload = {}
# first let create a dict with uid as a key and a list of infos as a value.
with open('test.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
data = {"locate": row['locate'], 'category': row['category'],
'act': row['act']}
if row['Uid'] in payload.keys():
payload[row['Uid']].append(data)
else:
payload[row['Uid']] = [data]
# reconstruct the payload to be list of dicts in the structure you want
result = list(map(lambda uid, infos: {'Uid':uid, 'infos':infos}, payload.items()))
I would change the resulting datastructure a bit so it becomes easier to handle:
result = {}
with open('test.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
if row['Uid'] not in result:
result[row['Uid']] = [{
'test_locate': row['locate'],
'test_category': row['category'],
'test_act': row['act']}]
else:
result[row['Uid']].append({
'test_locate': row['locate'],
'test_category': row['category'],
'test_act': row['act']})
print(result)
Your output looks like a list, but you need a dictionary. Try this:
result = {}
with open('test.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
uid = row['Uid']
del row['Uid']
if uid in result:
result[uid]['infos'].append(row)
else:
result[uid] = {'infos': [row]}
print(result)
The result you are showing is actually a list of dictionaries. If that is what you want, then:
result = []
with open('test.csv') as fp:
reader = csv.DictReader(fp)
infos = []
last_uid = None
for row in reader:
uid = row['Uid']
if uid != last_uid:
if last_uid is not None:
result.append({'Uid': last_uid, 'infos': infos})
last_uid = uid
infos = []
last_uid = uid
infos.append({'locate': row['locate'], 'category': row['category'], 'act': row['act']})
if last_uid is not None:
result.append({'Uid': last_uid, 'infos': infos})
So here is my issue, I have created a Dictionary for my lists and am trying to add the data I find and append it to each row but instead it is just appending to the same column with the same data type.
How could I get it so each new append add to new row.
data_dict = {'contact name': [], 'name': [], 'telephone': [], 'email': [],
'mobile': [], 'feedback average': []}
try:
data_dict['telephone'].append(soup.find('span',itemprop='telephone').text)
except AttributeError:
data_dict['telephone'].append('No telephone')
print data_dict
field_names = fn = data_dict.keys()
with open('./file.csv','w') as csvfile:
f = csv.DictWriter(csvfile, fieldnames=fn)
f.writeheader()
f.writerow(data_dict)
Try something like this:
data_dict = {'contact name': [], 'name': [], 'telephone': [], 'email': [],
'mobile': [], 'feedback average': []}
try:
data_dict['telephone'].append(soup.find('span',itemprop='telephone').text)
except AttributeError:
data_dict['telephone'].append('No telephone')
print data_dict
fn = data_dict.keys()
with open('./file.csv','w') as csvfile:
f = csv.reader(csvfile)
for row in f:
for i in len(fn):
data_dict[fn[i]].append(row[i])
This should work for you, if I got you right.
But care, this requires that one row in the csv contains exactly the elements of your dictionary, in the correct order.
If this isn't the case, you will need to find out which value is written in which column, and then add the value of this column to the list in your dictionary.
So you would need to replace
for i in len(fn):
data_dict[fn[i]].append(row[i])
by
for k in fn:
data_dict[k].append(row[columns[k]])
where columns is a dictionary that contains the same keys as data_dict, and as the values the columns in which the data of the specific key is stored in the csv-file. For an example, columns could look like this:
columns = {'contact name': 1, 'name': 3, 'telephone' : 6, 'email': 7, 'mobile':8, 'feedback average': 2}