I get the error in the title of this question. How can I fix that? The posts I found on Google did it like I did but it does not work for me.
Thank you for your help!
#!/usr/bin/python3
import os
import json
import datetime
import cgi
import time
def save(number_input, current_time):
i = 0
while os.path.exists("datei/datei{}.txt".format(i)):
i += 1
datei = {
"input": number_input,
"zeit": current_time
}
with open("datei/datei{}.txt".format(i), "w+") as file:
json.dump(datei, file)
form = cgi.FieldStorage(encoding="utf-8")
number = form.getvalue("first")
time = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")
save(number, time)
print("<p>Sie haben {} in einer .txt Datei gespeichert! </p>".format(number))
time.sleep(4)
print("Location: main.py")
print()
In this line:
time = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")
You are overwritting the time variable, that contained a module and making it a string.
This is an example of working code
#!/usr/bin/python3
import os
import json
import datetime
import cgi
import time
def save(number_input, current_time):
i = 0
while os.path.exists("datei/datei{}.txt".format(i)):
i += 1
datei = {
"input": number_input,
"zeit": current_time
}
with open("datei/datei{}.txt".format(i), "w+") as file:
json.dump(datei, file)
form = cgi.FieldStorage(encoding="utf-8")
number = form.getvalue("first")
time_str = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")
save(number, time)
print("<p>Sie haben {} in einer .txt Datei gespeichert! </p>".format(number))
time.sleep(4)
print("Location: main.py")
print()
Notice I've changed time to time_str, this way your time variable is untouched and you can call sleep :)
Related
In this program i am not using request or beautiful soup function. I'm instead only using the datetime to extract the URLs. Now in the current program, I have written to extract the values for a long period. I want to make it in such a way that, if I automate this program and it runs today, it will extract yesterday's data. Similarly if it runs tomorrow, it will extract todays data and so on.
here is the code,
import datetime
from datetime import date, datetime,timedelta
import warnings
import datetime
import pandas as pd
import wget
import glob
import os
warnings.filterwarnings("ignore")
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from urllib.error import HTTPError
def date_range(start_date,end_date):
for n in range(int((end_date-start_date).days)):
yield start_date + timedelta(n)
def get_urls(base_url):
part_two = "/dailyCoal1-"
end_part = ".xlsx"
start_date = date(2020,11,1)
end_date = datetime.datetime.now().date()
start_urls = list()
for single_date in date_range(start_date, end_date):
start_urls.append(single_date.strftime(base_url+'%d-%m-%Y'+part_two+'%Y-%m-%d'+end_part))
return start_urls
def excel_download(link,out):
#downloads a given link provided to a output directory in out
wget.download(link,out)
if __name__ =="__main__":
base_url = "https://npp.gov.in/public-reports/cea/daily/fuel/"
mypath = "/Users/vp/Desktop/temp"
temp_folder = '/Users/vp/Desktop/temp'
out_folder = "/Users/vp/Desktop/NPP"
log_file = os.path.join(out_folder,'debug_log_npp.log')
out_file = os.path.join(out_folder,'Energy_inputs_npp.csv')
file_links = get_urls(base_url)
for link in file_links:
try:
excel_download(link,temp_folder)
except HTTPError:
content = "HTTP issue while capturing data for this link - " + link
log_writer(log_file,content)
continue
file = glob.glob(os.path.join(temp_folder,'*.xlsx'),recursive=True)[0]
df = pd.read_excel(file)
To capture yesterday's data, i created this in the main function where i check for yesterday = and then cancel if it isnt yesterday. But then its throwing error as it constantly picks the start date as its day one.
if(date_time_obj != Yesterday):
os.remove(file)
content = "Date mis-matched - " + str(date_time_obj) + " " + str(Yesterday)
In this program, date_time_obj - is the date it is currently trying to extract data for.
Everyday if this program runs at 8pm, it needs to only capture one day before data on a daily basis.
if this cannot be done in datetime, but only on request or bs4, then how do i approach this problem?
I don't know if you wanted a valid link as your code doesn't seem to produce those for me but you only need to tweak to work off start_date only and return a single item to return yesterday's link matching with your current output for same date.
import datetime
from datetime import date, datetime,timedelta
import warnings
import datetime
import pandas as pd
import glob
import os
warnings.filterwarnings("ignore")
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from urllib.error import HTTPError
def get_url(base_url):
part_two = "/dailyCoal1-"
end_part = ".xlsx"
start_date = datetime.datetime.now().date() + timedelta(-1)
start_url = start_date.strftime(base_url+'%d-%m-%Y'+part_two+'%Y-%m-%d'+end_part)
return start_url
def excel_download(link,out):
#downloads a given link provided to a output directory in out
wget.download(link,out)
if __name__ =="__main__":
base_url = "https://npp.gov.in/public-reports/cea/daily/fuel/"
mypath = "/Users/vp/Desktop/temp"
temp_folder = '/Users/vp/Desktop/temp'
out_folder = "/Users/vp/Desktop/NPP"
log_file = os.path.join(out_folder,'debug_log_npp.log')
out_file = os.path.join(out_folder,'Energy_inputs_npp.csv')
file_link = get_url(base_url)
print(file_link)
I have made a web-scraper with python 3 and bs4. I want the current date so that i can use it as a file name for the scraped website.
Here is my code:
import bs4
import requests
import sys
import re
import unicodedata
import os
filename = #Current date#
filename=r"C:\Python\Scripts\Webscrapers\Output\\" +filename+ ".txt"
url = "https://www.wikipedia.org/Example_Article/"
res = requests.get(url)
soup = bs4.BeautifulSoup(res.text, "lxml")
file = open(filename , 'wb')
for i in soup.select("p"):
f=i.text
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", f)).encode('ascii', 'ignore'))
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", os.linesep)).encode('ascii', 'ignore'))
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", os.linesep)).encode('ascii', 'ignore'))
file.close()
After hours of googling i came up with this:
>>> import datetime
>>> print (datetime.datetime.today())
2020-05-14 11:49:55.695210
>>>
But,
I want something like this: 14-May-2020
Is it possible if so, then please help me
I just want to know the current date as a string
Use the strftime function from the time module:
import time
time.strftime("%d-%B-%Y", time.localtime())
'14-May-2020'
Here is my code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pydub import AudioSegment
import os
from urllib.parse import urlparse, parse_qs, unquote
from pytube import YouTube# misc
import urllib.request
from moviepy.editor import *
import eyed3
import time
class ConvertToMp3:
def convert(self,filename):
path_list = filename.split("/")
filename = path_list[len(path_list)-1]
path_list.remove(filename)
if(len(path_list)!=0 and path_list[0]==""):
path_list.remove(path_list[0])
path = ""
for folder in path_list:
path = path+"/"+folder
extension = filename.split(".")[1]
if path!="":
audio = AudioSegment.from_file(path+"/"+filename, format=extension)
else:
audio = AudioSegment.from_file(filename, format=extension)
audio.export("output files/"+filename.split(".")[0]+".mp3", format="mp3")
class ConvertFromYoutube:
def download(self,video_url,**options):
video_id = parse_qs(urlparse(video_url).query)['v'][0]
video = YouTube('https://www.youtube.com/watch?v='+video_id)
video.streams.get_by_itag(18).download("output files")
title = video.title
#print(title)
thumbnail = video.thumbnail_url
thumbnail_extension = thumbnail.split(".")
thumbnail_extension = thumbnail_extension[len(thumbnail_extension)-1]
urllib.request.urlretrieve(thumbnail, "output files/"+title+"."+thumbnail_extension)
self.save_as_mp3(title,"song_artist","song_album","song_album_artist","*****")
def save_as_mp3(self,song_title,song_artist,song_album,song_album_artist,song_rating):
video = VideoFileClip(os.path.join("output files",song_title+".mp4"))
video.audio.write_audiofile(os.path.join("output files",song_title+".mp3"))
audiofile = eyed3.load(os.path.join("output files",song_title+".mp3"))
audiofile.tag.artist = song_artist
audiofile.tag.album = song_album
audiofile.tag.album_artist = song_album_artist
audiofile.tag.title = song_title
print(audiofile.info.time_secs)
audiofile_duration = time.strftime('%H:%M:%S', time.gmtime(audiofile.info.time_secs))
print(audiofile.info.time_secs)
print(audiofile_duration)
audiofile.tag.save()
#test
#converter = ConvertToMp3()
#converter.convert("/home/chris/Μουσική/Απολυτίκιο Τριών Ιεραρχών.wav")
youtube = ConvertFromYoutube()
youtube.download("https://www.youtube.com/watch?v=JuYeHPFR3f0")
The time of pokemon theme song youtube video is: 3 minutes and 21 seconds.
The same information from file properties:
But print(audiofile.info.time_secs)
Prints out 405.62361067503923. That's wrong, so the calculation audiofile_duration = time.strftime('%H:%M:%S', time.gmtime(audiofile.info.time_secs)) is also wrong.
How can i fix that?
Thanks in advance,
Chris Pappas
Edit:
object_methods = [attr for attr in dir(audiofile.info) if not callable(getattr(audiofile.info, attr)) and not attr.startswith("__")]
print(object_methods)
['bit_rate', 'bit_rate_str', 'lame_tag', 'mode', 'mp3_header', 'sample_freq', 'size_bytes', 'time_secs', 'vbri_header', 'xing_header']
I found the solution here:
Finding the length of an mp3 file
But if it not possible to use so many libraries feel free for another answer.
I'm trying to print the actual time on an hourly based automated Email, but the program is repeating the first printed time again again. For Example, if the first rounds starts at 6:00 AM cst,again in the next email it is printing the same instead of 7:00, where I'm going wrong with the code below
import pytz
from datetime import datetime
import time
utc = timezone('UTC')
central = pytz.timezone('US/Central')
today = datetime.now(central)
crntDte = today.strftime("%m/%d/%Y")
crntTime = datetime.now(central).strftime('%H:%M')
def Action():
print('Process started at '+ crntDte + " " + crntTime)
schedule.every(2).minutes.do(Action)
while True:
schedule.run_pending()
time.sleep(1)
crntDte is working fine, but crnTime is printing the sametime everyhour
Well, your timezone looks not properly set. There are two timezone that are from pytz and datetime. I think you have to specify which one you will use.
import pytz
from datetime import datetime
import time
central = pytz.timezone('US/Central')
today = datetime.now(central)
crntDte = today.strftime("%m/%d/%Y")
crntTime = today.strftime('%H:%M')
print('Process started at '+ crntDte + " " + crntTime)
The result is then (for now)
Process started at 10/19/2019 09:18
When you do some schedule, then you have to re-calculate the time. So, your action should have all the definitions not only the print statement.
import pytz
from datetime import datetime
import time
def Action():
central = pytz.timezone('US/Central')
today = datetime.now(central)
crntDte = today.strftime("%m/%d/%Y")
crntTime = today.strftime('%H:%M')
print('Process started at '+ crntDte + " " + crntTime)
schedule.every(2).minutes.do(Action)
while True:
schedule.run_pending()
time.sleep(1)
There are not updated automatically.
import numpy as np
import os
import random
from six.moves import cPickle as pickle
from tensorflow.python.platform import gfile
import glob
import TensorflowUtils as utils
DATA_URL = 'http:\\data.csail.mit.edu\\places\\ADEchallenge\\ADEChallengeData2016.zip'
#download and read dataset
def read_dataset(data_dir):
pickle_filename = "MITSceneParsing.pickle"
pickle_filepath = os.path.join(data_dir, pickle_filename)
if not os.path.exists(pickle_filepath):
utils.maybe_download_and_extract(data_dir, DATA_URL, is_zipfile=True)
SceneParsing_folder = os.path.splitext(DATA_URL.split("/")[-1])[0]
result = create_image_lists(os.path.join(data_dir, SceneParsing_folder))
print ("Pickling ...")
with open(pickle_filepath, 'wb') as f:
pickle.dump(result, f, pickle.HIGHEST_PROTOCOL)
else:
print ("Found pickle file!")
with open(pickle_filepath, 'rb') as f:
result = pickle.load(f)
training_records = result['training']
validation_records = result['validation']
del result
return training_records, validation_records
train_records, valid_records = read_dataset('Data_zoo/MIT_SceneParsing')
print(len(train_records))
print(len(valid_records))
the result is:Found pickle file! 0 0
why the lens about train_records and valid_records are 0?
i don't know whree is wrong and how to correct it.
This code is right. The bug is in 'create_image_lists'.
Note this code in create_image_lists:
filename = os.path.splitext(f.split('/')[-1])[0]
This is no problem in Linux, but in windows, the separator is '\\', so you should modify this code to:
filename = os.path.splitext(f.split('\\')[-1])[0]
Then delete this file 'MITSceneParsing.pickle', and run read_dataset again.