Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New user seeking help
#1
As I am still working from home as part of the COVID-19 quarantine, I am trying to teach myself something new in this challenging time so please don't attack me if I seem really stupid, I am just trying to learn. There seems to be a lot of buzz about Python so I purchased a book titled, "Begin to Code with Python" by Rob Miles and have loaded v 3.7 on my desktop. As I starting thinking of things I want to learn with Python I downloaded information from Facebook to a file on my desktop. The file is in .html format and I would like to read each line in the file and change the order of the information and then save it as a .CSV file so I can do a statistical analysis of the data.

The input data has three components (see line below):
Ed liked Denise Robert’s comment. Jun 8, 2020, 5:58 PM

The first part shows an action I took in Facebook to "like" someone else's comment.
The second part did not show up as I cut and pasted but is a picture .
The third part is the date and time the action (ie Like) was taken.

As I started reading the book there are many things I have not really grasps yet and hope in time, with practice I will learn and understand.

I wrote the attached code which runs without any error messages but I am not accomplishing what I hope.
import html
import csv
import io

with open('D:/Facebook/likes_and_reactions/posts_and_comments.html', 'r',encoding='utf-8' ) as html_file, open('Test_Python.csv', 'w') as out_file:
    reader = html_file.readlines()
    writer = csv.writer(out_file)
print(writer)    

html_file.close()
out_file.close()
I would like to read each part as outlined about and then save: Part one, "a comma", Part three, "a comma" and then the picture reaction.

Any help or insights would be appreciated.
Reply
#2
Two things stand out immediately:

  1. No data are sent to the csv.write() to write to the file.
  2. The file.close() calls at the end are unneeded; the with statement closes them automatically when done.

Read up on csv module to determine how to send data to be written to the file. That will get you closer to your goal.
Reply
#3
Thank you for responding. I have tried a few different ways to resolve my problem but Python does not feel very intuitive for me. Could you please show me specifically how you would code the suggestions you have made and tell me why that would work?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking advice on dask distributed sawtooth500 4 265 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  seeking suggestions for function option name Skaperen 1 2,551 Dec-22-2018, 05:27 AM
Last Post: Gribouillis
  Newbie seeking help with DNS query DaytonJones 0 2,230 Sep-21-2018, 06:29 PM
Last Post: DaytonJones
  Seeking understanding with the python import function. Intelligent_Agent0 2 2,608 Feb-18-2018, 11:57 PM
Last Post: snippsat
  Seeking feedback on my script-in-progress league55 2 2,651 Feb-12-2018, 03:03 PM
Last Post: league55
  Seeking creative and knowlegeable coder for help! Elusth 4 5,473 Nov-07-2016, 08:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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