Python Forum
Print date, Time and output to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print date, Time and output to file
#1
Hello,
I am very green to python and currently wrote a code to look at a DB, look at the row for a date-time of the last backup. I'm now trying to figure out how to print the data to text file, print it console and find a function to email the file out. I was working on trying to get the UID PW for a call to file but I gave up on that one. Can anyone provide me some help or guidance on completing these tasks? Im sure there is a better/simplified way to my code but again I am just learning via googling how to do things.


#! /usr/bin/python3.6

import pymysql
import datetime
date = str(datetime.date.today()-datetime.timedelta(1))
print(date)
 
 
 
try:
    db_con1 = pymysql.connect(host='nhmanpncblt01.xx.xxxx.com', port=3306, user='xxxx', passwd='xxxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con2 = pymysql.connect(host='nhmanpnckey01.xx.xxxx.com', port=3306, user='xxxx', passwd='xxxxx!', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con3 = pymysql.connect(host='nhmanpncfre01.xx.xxxxt.com', port=3306, user='xxxx', passwd='xxxxx', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    db_con4 = pymysql.connect(host='NHMANPNCNE01.xx.xxxx.com', port=3306, user='xxxxxx', passwd='xxxx!!', db='photonicnetworkcaptures')
    sql_select_Query = "select * from pncnetwork where startTime regexp \'^{}\'".format(date)
    cursor1 = db_con1.cursor()
    cursor2 = db_con2.cursor()
    cursor3 = db_con3.cursor()
    cursor4 = db_con4.cursor()
    cursor1.execute(sql_select_Query)
    cursor2.execute(sql_select_Query)
    cursor3.execute(sql_select_Query)
    cursor4.execute(sql_select_Query)
    records1 = cursor1.fetchall()
    records2 = cursor2.fetchall()
    records3 = cursor3.fetchall()
    records4 = cursor4.fetchall()
    print ("Total number of rows in pncnetwork is: ", cursor1.rowcount + cursor2.rowcount + cursor3.rowcount + cursor4.rowcount)
    print ("\nPrinting each pncnetwork record")
    for row in records1:
        print("{}, {}".format("Beltway", row[2], row[3]))
    for row in records2:
        print("{}, {}".format("Keystone", row[2], row[3]))
    for row in records3:
        print("{}, {}".format("Freedom", row[2], row[3]))
    for row in records4:
        print("{}, {}".format("New England", row[2], row[3]))
except Exception as e:
    print("ERROR ", str(e))
 
finally:
    db_con1.close()
    db_con2.close()
    db_con3.close()
    db_con4.close()
Reply


Messages In This Thread
Print date, Time and output to file - by tpolim008 - Mar-26-2020, 03:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 255 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 382 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 628 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,060 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,306 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Print names in x-axis of a time-series values hobbyist 4 1,248 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,119 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,109 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,353 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Saving the print result in a text file Calli 8 1,824 Sep-25-2022, 06:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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