Python Forum
How to store python output into a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to store python output into a file?
#1
hey guys,

i've recently finished a web crawler app for an ads website that prints the ad title, ad link and the number of views each ad has.
the crawler uses chromedriver which is an app that executes javascript on a website to extract information off of it as opposed to getting it out of the source code with beautifulsoup, data that's not stored in the source code otherwise.
after the app prints all that it goes to the next ad an so on.

my question is: i'm trying to store all the output into a file that i can access and have a bird's eye view of and i'm trying to figure out the best way to go about it.
i thought initially of excel because i've been getting ads off of the website manually anyway and stored them into an excel for a while now but i'm having trouble finding the best module to work with.
i looked into openpyxl but i can't seem to find any tutorials on it to help me learn how to use it.
then i looked into pandas because it comes up a lot but all the videos are aimed at data processing which doesn't really apply to my case.
i also tried xlwt but it's kinda old now and it doesn't handle .xlsx files.

and i'm kinda running out of ideas and i though i'd ask you guys about it, maybe you know a good excel module out there that also has tutorials or if there's any better way of storing data other than excel.

thanks
Reply
#2
You can find some sample code here: http://nullege.com/codes/search?cq=import+openpyxl
which may help
Reply
#3
script.py > file.txt
I think it works on Windows the same way
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
@Larz60+: that, is scary.
@wavic: what i'm trying to do is print the program output; it already goes in there and grabs all that info and prints it out in the console; i'm trying to store the output into an excel file not the code. i already have the code (wrote it myself, with help from you guys) :)

anyway, i tried playing around with it and i think i'll go with openpyxl eventually but i am vanig a couple of issues with it right now.
i made a test .xlsx file so i can play with loading files into it but i can't seem to be able to display values off of it. i tried this:

from openpyxl import load_workbook

file = load_workbook('file.xlsx', data_only=True)
c = file['A1':'C4']

print(c.value)
but i get a bunch of errors:

Output:
Traceback (most recent call last):   File "C:/Users/asdasd/Desktop/test/outputtest.py", line 5, in <module>     c = file['A1':'C4']   File "C:\Program Files (x86)\Python35-32\lib\site-packages\openpyxl\workbook\workbook.py", line 227, in __getitem__     raise KeyError("Worksheet {0} does not exist.".format(key)) KeyError: "Worksheet slice('A1', 'C4', None) does not exist." Process finished with exit code 1
i checked the module documentation but i'm nut sure where my error is.
any ideas how to print cell values with this module?
Reply
#5
This is output redirection. As usual the output of a command is on the terminal window. But that way command > output.txt it writes it into a file instead on the terminal. 
So you want to write the data into excel file. Not just the output?

Here
is the documentation for openpyxl


A documentation for XlsxWriter module
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Just create simple CSV file. Excel will open it without problems
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Python Obstacles | Kung-Fu | Full File HTML Document Scrape and Store it in MariaDB BrandonKastning 5 2,816 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  Python Obstacles | American Kenpo | Wiki Scrape URL/Table and Store it in MariaDB BrandonKastning 6 2,782 Dec-29-2021, 12:38 AM
Last Post: BrandonKastning
  Python Obstacles | Karate | HTML/Scrape Specific Tag and Store it in MariaDB BrandonKastning 8 3,090 Nov-22-2021, 01:38 AM
Last Post: BrandonKastning
  read text file using python and display its output to html using django amit 0 18,261 Jul-23-2017, 06:14 AM
Last Post: amit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020