I'm attempting to write a script that allows me to read data from an input excel file (saved in .csv format because someone told me to do it that way), and write selected portions of this data to a word document.
It is a script to create personalised delivery notes for participants' meal choices (the choices are cumulatively added to an input spreadsheet).
So far I have created a dummy input spreadsheet, and saved a blank dummy output word file (dummy.csv and dummy.txt, respectively).
The code I have written so far reads the spreadsheet into the terminal, with some formatting to tidy it up.
import csv
f = open("dummy.csv")
csv_f = csv.reader(f)
for row in csv_f:
print('{:<15} {:<15} {:<20} {:<25}'.format(*row))
The output looks like this: (Dummy meal choices kept the same for ease)
Participant ID Breakfasts Lunches/dinners Snacks
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Full english Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
1111 Risotto Granola
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Avocado toast Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
2222 Bean chilli Apple
My next challenge is to somehow write this data to a word file for participant 1111, another for participant 2222, and so on. I don't want the script to necessarily write the exact data from these rows to the word file, but rather whatever data could be on these rows should the food choices in the input file be different.
It would be good to keep the meals split into Breakfasts, Lunches/dinners, and Snacks on the output delivery note.
I can tidy up font etc later, I just want the meal selections to be there for now. I'll also want to have it say "7 x Full english", rather than "Full english, Full english, Full english etc."
Thank you for reading, any help would be hugely appreciated!
Kieran
Just to show where you exemplary could go using pandas
:
import pandas as pd
df = pd.read_csv('whereverfilemayroam/filename')
Participant ID Breakfasts Lunches/dinners Snacks
0 1111 Full english Risotto Granola
1 1111 Full english Risotto Granola
2 1111 Full english Risotto Granola
3 1111 Full english Risotto Granola
4 1111 Full english Risotto Granola
5 1111 Full english Risotto Granola
6 1111 Full english Risotto Granola
7 1111 None Risotto Granola
8 1111 None Risotto Granola
9 1111 None Risotto Granola
10 1111 None Risotto Granola
11 1111 None Risotto Granola
12 1111 None Risotto Granola
13 1111 None Risotto Granola
14 2222 Avocado toast Bean chilli Apple
15 2222 Avocado toast Bean chilli Apple
16 2222 Avocado toast Bean chilli Apple
17 2222 Avocado toast Bean chilli Apple
18 2222 Avocado toast Bean chilli Apple
19 2222 Avocado toast Bean chilli Apple
20 2222 Avocado toast Bean chilli Apple
21 2222 None Bean chilli Apple
22 2222 None Bean chilli Apple
23 2222 None Bean chilli Apple
24 2222 None Bean chilli Apple
25 2222 None Bean chilli Apple
26 2222 None Bean chilli Apple
27 2222 None Bean chilli Apple
That's your file in a pandas dataframe, the standard container in pandas, if you like. And now you can make a plenty of statistical things with it... Just dig a little through the docs
Examples:
df.groupby(['Participant ID', 'Breakfasts']).Breakfasts.count()
Participant ID Breakfasts
1111 Full english 7
2222 Avocado toast 7
Name: Breakfasts, dtype: int64
df.groupby(['Participant ID', 'Lunches/dinners'])['Lunches/dinners'].count()
Participant ID Lunches/dinners
1111 Risotto 14
2222 Bean chilli 14
Name: Lunches/dinners, dtype: int64
Of course you can separate by Participant ID:
oneoneoneone = df[df['Participant ID'] == 1111]
oneoneoneone
Participant ID Breakfasts Lunches/dinners Snacks
0 1111 Full english Risotto Granola
1 1111 Full english Risotto Granola
2 1111 Full english Risotto Granola
3 1111 Full english Risotto Granola
4 1111 Full english Risotto Granola
5 1111 Full english Risotto Granola
6 1111 Full english Risotto Granola
7 1111 None Risotto Granola
8 1111 None Risotto Granola
9 1111 None Risotto Granola
10 1111 None Risotto Granola
11 1111 None Risotto Granola
12 1111 None Risotto Granola
13 1111 None Risotto Granola
oneoneoneone.to_csv('target_file')
and so would perhaps
twotwotwotwo.to_csv('another_target_file')
One can also iterate over groups, and then apply always the same operations on each group.
eg:
for name, group in df.groupby('Participant ID'):
print(name)
print(group.groupby('Breakfasts').Breakfasts.count().to_string())
print(group.groupby('Lunches/dinners')['Lunches/dinners'].count().to_string())
print(group.groupby('Snacks').Snacks.count().to_string(), '\n')
returns:
1111
Breakfasts
Full english 7
Lunches/dinners
Risotto 14
Snacks
Granola 14
2222
Breakfasts
Avocado toast 7
Lunches/dinners
Bean chilli 14
Snacks
Apple 14
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.