[英]Transferring data from excel to word in python 3
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. 我正在尝试编写一个脚本,该脚本允许我从输入的excel文件中读取数据(保存为.csv格式,因为有人告诉我这样做),然后将这些数据的选定部分写入到word文档中。
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). 到目前为止,我已经创建了一个虚拟输入电子表格,并保存了一个空白的虚拟输出字文件(分别为dummy.csv和dummy.txt)。
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. 我的下一个挑战是以某种方式将此数据写入到参与者1111的单词文件中,另一个写入参与者2222的单词文件中,依此类推。 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.
我不希望脚本将这些行中的确切数据写入Word文件,而是如果输入文件中的食物选择不同,则这些行中的数据可以是什么。
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."
我也想说“ 7 x Full english”,而不是“ Full english,Full english,Full english等”。
Thank you for reading, any help would be hugely appreciated! 感谢您的阅读,我们将不胜感激!
Kieran 基兰
Just to show where you exemplary could go using pandas
: 只是为了展示您可以使用
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. 如果您愿意,这就是您在pandas数据框中的文件,pandas中的标准容器。 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: 当然,您可以按参与者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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.