store data which is an output of python program in

2019-09-07 00:45发布

ענת מאירה

0546515015

2016-07-25

בוטוקס ועיבוי שפתיים.


מחמוד אבו חב

0505178463

2016-07-25

מתעניין בעורך דין לכל מטרה.


אנעאם אבו חב

0542969636

2016-07-25

מתעניין בעורך דין לכל מטרה.


this is an output got from python scrapping I want to store it into excel/csv file, such as each row contain 4 columns

for example

+-------------------------------------------------------+

| col 1 | col 2 | col 3 | col 4 |

|בוטוקס ועיבוי שפתיים|0546515015|2016-07-25"|ענת מאירה|

+------------------------------------------------------+

how can I achieve it..? Please Help... Thanks in Advance :)

3条回答
劳资没心,怎么记你
2楼-- · 2019-09-07 01:04

you could just do this on commandline

test.py>output.csv 

This will put all the data in a csv file which can then be read by excel

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-07 01:04

Ok so you can use openpyxl.

This is one way to proceed. I assume that each line of output that you have is stored as a variable or something. Maybe something like this and you loop through it:

varCol1 = "ענת מאירה"
varCol2 = "2016-07-25"
varCol3 = "0546515015"
varCol4 = "בוטוקס ועיבוי שפתיים"

So to get each of those variables written to a spreadsheet you could do something like this:

import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet['A1'] = varCol1 
sheet['B1'] = varCol2
sheet['C1'] = varCol3
sheet['D1'] = varCol4
wb.save('example_excel.xlsx')

This type of thing could be implemented in a loop and you could iterate through the row number to fill the sheet with the data that you want. For more help check out This Link.

查看更多
Ridiculous、
4楼-- · 2019-09-07 01:23

Start here - https://docs.python.org/3.5/library/csv.html. This is standard library for writing csv files. For Excel files you can try openpyxl, which have to be installed separately.

查看更多
登录 后发表回答