Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Last active April 2, 2024 21:46
Show Gist options
  • Save juanarrivillaga/b3ae481e896c4a5dafdc582de8365550 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/b3ae481e896c4a5dafdc582de8365550 to your computer and use it in GitHub Desktop.
# So, assuming the lists have the same length, e.g.:
dummy_data = {
"Date":["01/24/2029","10/28/2027", "01/24/2024","03/24/2024"],
"Company Name":["Mcdonalds", "Burger King", "KFC","Popeyes"],
"File_name_location":["C/Documents/Files/invoice1.pdf", "C/Documents/Files/invoice1.pdf","C/Documents/Files/invoice1.pdf", "C/Documents/Files/invoice1.pdf"],
}
# Then, to sort everything, simply get a list of sorted indices.
# This can be done easily. Since you don't want `datetime.date` objects
# (for some reason - you *should* probably just use them and then when you want a string simply use the `datetime.date`
# formatting tools to create whatever string you want).
sorted_indices = sorted(
range(len(dummy_data['Date'])),
key=lambda i: datetime.datetime.strptime(dummy_data['Date'][i], "%m/%d/%Y")
)
# Then finally, use the indices to re-arrange the lists in the dict:
for k, vs in dummy_data.items():
dummy_data[k] = [vs[i] for i in sorted_indices]
# Which would give the following output:
"""
{'Date': ['01/24/2024', '03/24/2024', '10/28/2027', '01/24/2029'],
'Company Name': ['KFC', 'Popeyes', 'Burger King', 'Mcdonalds'],
'File_name_location': ['C/Documents/Files/invoice1.pdf',
'C/Documents/Files/invoice1.pdf',
'C/Documents/Files/invoice1.pdf',
'C/Documents/Files/invoice1.pdf']}
"""
# also, you don't need pandas just to make a csv. that's like swatting a fly with a sledgehammer
import csv
with open('output.csv', 'w', newline="") as f:
writer = csv.writer(f)
# write the header from the keys
writer.writerow(list(dummy_data))
# write the rest of the data in the lists
writer.writerows(zip(*dummy_data.values()))
@Kevsosmooth
Copy link

for the csv I will look into this option because I did want to optimize my code just a bit and this way would save time and resouces the issue is that this is how I saw people do it online
and also would does the * mean after zip?

@juanarrivillaga
Copy link
Author

juanarrivillaga commented Apr 2, 2024

@Kevsosmooth * is iterable unpacking. See: https://stackoverflow.com/questions/2921847/what-do-double-star-asterisk-and-star-asterisk-mean-in-a-function-call. This is often used with zip to effect a transpose, from "columns" into "rows":

>>> data = [[1,2,3],['a', 'b', 'c'], [1.0, 2.0, 3.0]]
>>> list(zip(*data))
[(1, 'a', 1.0), (2, 'b', 2.0), (3, 'c', 3.0)]
>>> list(zip(data[0], data[1], data[2]))
[(1, 'a', 1.0), (2, 'b', 2.0), (3, 'c', 3.0)]

@Kevsosmooth
Copy link

I understand would you be able to help me use the code that you have to write the data with the import csv but on a list of dicts for example

example_list = [ {"DATE":"1/24/2024", "Company":"ABC"}, {"DATE":"1/24/2024", "Company":"ABC"}, {"DATE":"1/24/2024", "Company":"ABC"} ]

import csv
with open("test.csv","w", newLine ="") as f
writer = csv.writer(f)

What would I have to write to make the example list use Date as the column headers and dates as the dates and also COMPANY

I did try using your method but it seems yours is converting the dict into a list but now with this new way I am doing it this means my list is already a list just of dicts versus the opposite where dummy_data is a dict which contains lists

In any case would be great help but I am going to most likely look online.

@Kevsosmooth
Copy link

import csv

test_data = [{"Company Name":"ABC", "Date":"10/24/2024"},
{"Company Name":"KKA", "Date":"2/24/2024"},
{"Company Name":"DDD", "Date":"3/24/2024"},
{"Company Name":"CCC", "Date":"8/24/2024"}]

keys = test_data[0].keys()
print(keys)

with open("silvi.csv", "w", newline='') as f:
dict_writer = csv.DictWriter(f, keys)
dict_writer.writeheader()
dict_writer.writerows(test_data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment