Dictionary to CSV using Python with headers style - python

For this same Stack Overflow question How do I output a list of dictionaries to an Excel sheet?.
For xlsx code by jossef-harush we can use this:
import xlsxwriter
# ...
def create_xlsx_file(file_path: str, headers: dict, items: list):
with xlsxwriter.Workbook(file_path) as workbook:
worksheet = workbook.add_worksheet()
worksheet.write_row(row=0, col=0, data=headers.values())
header_keys = list(headers.keys())
for index, item in enumerate(items):
row = map(lambda field_id: item.get(field_id, ''), header_keys)
worksheet.write_row(row=index + 1, col=0, data=row)
headers = {
'bank': 'Money in Bank',
'dailyWinners': 'Daily Winners',
'dailyFree': 'Daily Free',
'user': 'User',
}
players = [
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player1', 'bank': 0.06},
{'dailyWinners': 3, 'dailyFreePlayed': 2, 'user': 'Player2', 'bank': 4.0},
{'dailyWinners': 1, 'dailyFree': 2, 'user': 'Player3', 'bank': 3.1},
{'dailyWinners': 3, 'dailyFree': 2, 'user': 'Player4', 'bank': 0.32}
]
create_xlsx_file("my xslx file.xlsx", headers, players)
For creating a CSV file, what are the changes I need to do in the above code?
Like create_csv_file("my csv file.csv", headers, players).

import csv
def create_csv_file(file_path, headers, items):
with open(file_path, "wt") as f:
dw = csv.DictWriter(f, headers.values(), extrasaction='ignore')
dw.writeheader()
for row in items:
dw.writerow({headers.get(k): v for (k, v) in row.items()})
writes
Money in Bank,Daily Winners,Daily Free,User
0.06,3,,Player1
4.0,3,,Player2
3.1,1,2,Player3
0.32,3,2,Player4
Note that this will ignore any dict keys not in headers; in your case, the dailyFreePlayeds.

Related

unable to write tuple into xslx file using python without pandas?

I am trying to write the output into xslx file, but able to only write the headers not able to write the data below headers.
import xlsxwriter
csv_columns = (
'id', 'name', 'place', 'salary', 'email',
)
details = [{'id':1, 'name': 'A', 'place':'B', 'salary': 2, 'email': 'c#d.com'},
{'id':3, 'name':'C', 'place':'D', 'salary': 4, 'email':'e#f.com'}]
workbook = xlsxwriter.Workbook(path)
worksheet = workbook.add_worksheet()
for col, name in enumerate(csv_columns):
worksheet.write(0, col, name)
for row, det in enumerate(details, 1):
for col, value in enumerate(det):
worksheet.write(row, col, value)
workbook.close()
This code is only writing the csv_columns in xslx file and repeating same in all rows as below
id name place salary email
id name place salary email
id name place salary email
How to solve this issue of repeating columns in xslx? any help ?
I expected like below:
id name place salary email
1 A B 2 c#d.com
3 C D 4 e#f.com
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
csv_columns = (
'id', 'name', 'place', 'salary', 'email',
)
details = [{'id':1, 'name': 'A', 'place':'B', 'salary': 2, 'email': 'c#d.com'},
{'id':3, 'name':'C', 'place':'D', 'salary': 4, 'email':'e#f.com'}]
details_values = [tuple(d.values()) for d in details]
details_values.insert(0, csv_columns)
for row in details_values:
print(row)
ws.append(row)
wb.save(output_file_path)
I corrected your code. Now it works as you would expect:
import xlsxwriter
csv_columns = (
'id', 'name', 'place', 'salary', 'email',
)
values = [(1, 'A', 'B', 2, 'c#d.com'),
(3, 'C', 'D', 4, 'e#f.com')]
workbook = xlsxwriter.Workbook(path)
worksheet = workbook.add_worksheet()
row, col = 0, 0
worksheet.write_row(row, col, csv_columns)
row += 1
for value in values:
worksheet.write_row(row, col, value)
row += 1
workbook.close()
It would probably be best to map your dictionaries into a list of lists and then process it that way, but here is one way of doing it based on your sample code:
import xlsxwriter
csv_columns = ('id', 'name', 'place', 'salary', 'email')
details = [{'id': 1, 'name': 'A', 'place': 'B', 'salary': 2, 'email': 'c#d.com'},
{'id': 3, 'name': 'C', 'place': 'D', 'salary': 4, 'email': 'e#f.com'}]
workbook = xlsxwriter.Workbook("test.xlsx")
worksheet = workbook.add_worksheet()
worksheet.write_row(0, 0, csv_columns)
for row, det in enumerate(details, 1):
for col, key in enumerate(csv_columns):
worksheet.write(row, col, det.get(key, ''))
workbook.close()
Output:

Not able to convert json data into csv in python while fetching data through api

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

python csv convert to nest dictionary

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})

Storing dictionary data using Python3 and SQLlite

Here is my code:
def getDownloaders(dbPATH):
with sqlite3.connect(dbPATH) as db:
cursor = db.cursor()
cursor.execute("SELECT * FROM Downloaders")
d = cursor.fetchall()
downloader = {}
column_names = [s[0] for s in cursor.description]
for i in range(len(d)):
for row in d:
downloader[i] = dict(zip(column_names, row))
print(downloader)
return downloader
Here is my data:
[{1, 'lll', ‘lll', 'lll', '', ‘1’, 'lobio', 'c:/'},
{2, 'test', ‘test3', 'blob', 'blah', ‘1’, 'lio', 'c:/'},
{3, 'ledere', ‘copsssss', 'reds', 'server', ‘0’, 'lobio', 'c:/'}]
Here is what I want in a dictionary
{0: {'id': 1, 'Host': 'lll', 'username': 'lll', 'password': 'lll', 'label': 'lll', 'Enabled': 1, 'name': 'lobio', 'file': 'c:/'}, 1: {'id': 2,'Host': 'test', 'username': 'test3', 'password': 'blob', 'label': 'blah', 'Enabled': 1, 'name': 'lio', 'file': 'c:/'}, 2: {'id': 3, 'Host': 'lwderel', 'username': ‘copsssss', 'password': 'reds', 'label': 'server', 'Enabled': 0, 'name': 'lobio', 'file': 'c:/'}}
You have two nested for loops, for all row indexes, and for all rows, so the innermost line sees all combinations of i and row (3×3), even those where these two do not match.
You have to use a single loop:
cursor.execute("...")
column_names = [s[0] for s in cursor.description]
downloader = {}
i = 0
for row in cursor:
downloader[i] = dict(zip(column_names, row))
i += 1
And a dictionary with consecutive numbers as keys is pointless; it would be simpler to use an array as return value:
cursor.execute("...")
column_names = [s[0] for s in cursor.description]
downloader = [dict(zip(column_names, row)) for row in cursor]

Python before writing to CSV file check if column data is present

Hi have dictionary data as shown:
{'Count': 5, '_id': {'ele_id': ['17cd-4a9f-9671-80eda11f9c53'], 'day': '2015-09-22'}, 'name': 'Default Astn'}
{'Count': 2, '_id': {'ele_id': ['17cd-4a9f-9671-80eda11f9c53'], 'day': '2015-09-18'}, 'name': 'Default Astn'}
{'Count': 1, '_id': {'ele_id': ['ccdf-4e0b-a87c-4e7738a0ed33'], 'day': '2015-09-14'}, 'name': 'sharepoint Astn'}
{'Count': 1, '_id': {'ele_id': ['2b9f-436b-a2ff-c4bc4059a9c8'], 'day': '2015-09-14'}, 'name': 'JPL Astn'}
{'Count': 2, '_id': {'ele_id': ['17cd-4a9f-9671-80eda11f9c53'], 'day': '2015-09-14'}, 'name': 'Default Astn'}
Want to write to CSV with columns and data as below:
Date Name Count
2015-09-22 Default Astn 5
2015-09-18 Default Astn 2
2015-09-14 sharepoint Astn 1
JPL Astn 1
Default Astn 2
Problem I'm facing is for 3 row, just add 2 and 3rd column if 1st column is already same.
My code is as below
with open('test.csv', 'wb') as f:
fieldnames = ['Date','Name','Count']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for line in data:
writer.writerow({'Date' : line['_id']['day'],'Name' : line['name'], 'Count':line['Count']})
Try this ..... haven't tested but I think the logic remains the same......
with open('test.csv', 'wb') as f:
fieldnames = ['Date','Name','Count']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
prev_date = ''
for line in data:
curr_date = line['_id']['day']
if curr_date == prev_date:
writer.writerow({'Date' : '','Name' : line['name'], 'Count':line['Count']})
else:
writer.writerow({'Date' : curr_date,'Name' : line['name'], 'Count':line['Count']})
prev_date = curr_date

Categories