Ignore unconverted data from date-time stamp when using strptime - python-3.5

A third party API returns me a CSV data containing date time stamp like this:
dtval = '2016-10-14 05:09:30+00:00'
I have to convert it in the format : mm/dd/yyyy.
Here I'm not sure about last +XX:XX of directive:
datetime.datetime.strptime(dtval, "%Y-%m-%d %H:%M:%S+XX:XX").strftime('%m/%d/%Y')
I tried followings but did not work:
>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', '%Y-%m-%d %H:%M:%S')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 340, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: +00:00
>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', "%Y-%m-%d %H:%M:%S%z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '2016-10-14 05:09:30+00:00' does not match format '%Y-%m-%d %H:%M:%S+%z'
Is there any option in the Python3.4+'s datetime module to ignore remainging unconverted data?
I gone through this but did not find any such option

After a little bit research I found this fix in django's source code :
class FixedOffset(tzinfo):
"""
Fixed offset in minutes east from UTC. Taken from Python's docs.
Kept as close as possible to the reference version. __init__ was changed
to make its arguments optional, according to Python's requirement that
tzinfo subclasses can be instantiated without arguments.
"""
def __init__(self, offset=None, name=None):
if offset is not None:
self.__offset = timedelta(minutes=offset)
if name is not None:
self.__name = name
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return self.__name
def dst(self, dt):
return timedelta(0)
def get_timezone(offset):
"""
Returns a tzinfo instance with a fixed offset from UTC.
"""
if isinstance(offset, timedelta):
offset = offset.seconds // 60
sign = '-' if offset < 0 else '+'
hhmm = '%02d%02d' % divmod(abs(offset), 60)
name = sign + hhmm
return FixedOffset(offset, name)
def custom_strptime(self, value):
"""Parses a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
Raises ValueError if the input is well formatted but not a valid datetime.
Returns None if the input isn't well formatted.
"""
datetime_re = re.compile(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
)
match = datetime_re.match(value)
if match:
kw = match.groupdict()
if kw['microsecond']:
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = get_timezone(offset)
kw = {k: int(v) for k, v in kw.items() if v is not None}
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)

Related

Attribute error while scraping gdelt data

I am scraping data from GDELT [https://www.gdeltproject.org]. It is a pretty cool project that checks ~100,000 news sites each day, labels all the articles, and makes them available. I am getting attribute error while extracting the data. The code use is the following:
import gdelt
gd = gdelt.gdelt(version=1)
from statsmodels.tsa.api import VAR
import pandas as pd
import os
os.makedirs("data",exist_ok=True)
import datetime
cur_date = datetime.datetime(2022,1,10) - datetime.timedelta(days=10)
end_date = datetime.datetime(2022,1,10)
year = cur_date.year
month = str(cur_date.month)
day = str(cur_date.day)
if cur_date.month < 10:
month = "0" + month
if cur_date.day < 10:
day = "0" + day
gd.Search(['%s %s %s'%(year, month, day)],table='gkg',coverage=True, translation=False)
I am getting attribute error
AttributeError Traceback (most recent call last)
<ipython-input-10-2f00cabbf1ac> in <module>
----> 1 results = gd.Search(['%s %s %s'%(year, month, day)],table='gkg',coverage=True,
translation=False)
~\anaconda3\lib\site-packages\gdelt\base.py in Search(self, date, table, coverage,
translation, output, queryTime, normcols)
646
647 if self.table == 'gkg' and self.version == 1:
--> 648 results.columns = results.ix[0].values.tolist()
649 results.drop([0], inplace=True)
650 columns = results.columns
~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5463 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5464 return self[name]
-> 5465 return object.__getattribute__(self, name)
5466
5467 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'ix'

i am trying to calculate sentimental score of each syntax of csv file by using csv library

this is the error which i am getting. In the previous post i forget to put both function . In the first function i'm reading csv file and removing punctuation and send the string to second function to calculate the sentimentel score. this code give output for few row of csv file and then show this error i'm new in python
Traceback (most recent call last):
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 116, in <module>
Countswordofeachsyntax()
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 92, in Countswordofeachsyntax
print(findsentimentalscore(nopunct))
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 111, in findsentimentalscore
ss =ss + weight
TypeError: unsupported operand type(s) for +: 'int' and 'list'
def Countswordofeachsyntax():
nopunct = ""
with open('dataset-CalheirosMoroRita-2017.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='|')
for sys in csv_reader:
for value in sys:
nopunct = ""
for ch in value:
if ch not in punctuation:
nopunct = nopunct + ch
print(findsentimentalscore(nopunct))
def findsentimentalscore(st):
ss = 0
count = len(st.split())
mycollapsedstring = ' '.join(st.split())
print(str(mycollapsedstring.split(' ')) + " := " + str(len(mycollapsedstring.split())))
for key, weight in keywords.items():
if key in mycollapsedstring.lower():
ss =ss + weight
#print(key, weight)
res = (ss / count * 100)
return math.ceil(res)

Unable to a convert given date to a required date format that is coming from the df['dateformat'] In python

MeetingDate = datetime.datetime(2020,5,11)
df = pd.read_csv('request.csv')
dateformatsXpaths = df['dateformat']
MeetingDateXpaths = df['WebCfg_MtgDate_xpath']
urls = df['url']
fcids = df['fcid']
for url in urls:
driver.get(url)
agendaMeetingdateformat = MeetingDate.strftime(df['dateformat'])
meeting =df['WebCfg_MtgDate_xpath'].replace('*', agendaMeetingdateformat)
my_element = driver.find_element_by_xpath(meeting).text
if my_element == agendaMeetingdateformat :
print('valid Meeting date')
else:
print('Invalid Meeting date')
driver.close()
I want to convert the given date to the date format that is coming from the df['dateformat'],but its throwing following error
Traceback (most recent call last):
File "C:\Users\sameer\PycharmProjects\Python\MeetingDateFinder.py", line 32, in <module>
agendaMeetingdateformat = MeetingDate.strftime(df['datefromat'])
TypeError: strftime() argument 1 must be str, not Series

Comparing two time types in Python

I'm trying to make a function that checks the current time, and tells me if it's before or after sunrise, sunset, dusk, dawn and noon.
But I'm a bit stuck in how to compare the time types I get :(
Can anyone help me out?
This is my code:
now = datetime.now()
now_time = now.time()
print ('\nTime now is %s \n' % now_time)
city_name = 'Stockholm'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
print('Information for %s/%s\n' % (city_name, city.region))
timezone = city.timezone
print('Timezone: %s' % timezone)
print('Latitude: %.02f; Longitude: %.02f\n' % \
(city.latitude, city.longitude))
today = datetime.strptime(time.strftime("%Y-%m-%d"), '%Y-%m-%d')
sun = city.sun(date=datetime.date(today), local=True)
dawn = str(sun['dawn'])[11:-6]
sunrise = str(sun['sunrise'])[11:-6]
noon = str(sun['noon'])[11:-6]
sunset = str(sun['sunset'])[11:-6]
dusk = str(sun['dusk'])[11:-6]
print('Dawn: %s' % dawn)
print('Sunrise: %s' % sunrise)
print('Noon: %s' % noon)
print('Sunset: %s' % sunset)
print('Dusk: %s \n' % dusk)
if now_time > datetime.strptime(dawn, '%H:%M:%S'):
print ('Time is after dawn')
else:
print ('Time is before dawn')
It returns this:
Traceback (most recent call last):
File "test.py", line 63, in <module>
If now_time > datetime.strptime(dawn, '%H:%M:%S'):
TypeError: can't compare datetime.time to datetime.datetime
To extract a time object from a datetime object, use the datetime.time() function or the datetime.timetz() function. The latter will retain timezone information, while the former will not.
So change the problem line to this:
if now_time > datetime.strptime(dawn, '%H:%M:%S').time():

My program is supposed to output answers into a csv file with a certain format

This is the code
import datetime
mylist = []
today = datetime.date.today()
mylist.append(today)
print(mylist[0])
file = open('Date.txt', 'a')
file.write(mylist)
file.close()
global timestable
global text
def timestable():
global name
name = input("What is your name? ")
global number
number = int(input("Insert a number you want the 12 times table for: "))
global multiply
for multiply in range(1,13):
print(number,"x",multiply,"=",number*multiply)
text()
def text():
file=open("list.csv","a")
file.write("The date is," + today + "\nTimestables are:\n" + number + "x" + multiply + "=" + number*multiply + "\nYour name is, " + name)
file.close()
text()
timestable()
The problem is that nothing is being outputted into the file and its supposed to be outputted with a certain format also.
The error is
Traceback (most recent call last):
File "D:\Python\Code\Lesson Task Code.py", line 9, in <module>
file.write(mylist)
TypeError: must be str, not list
Instead of datetime.date.today() you probably want to tryout time.strftime("%Y-%m-%d"):
from time import strftime
today = strftime("%Y-%m-%d")
print(today)
with open('Date.txt', 'a') as file:
file.write(today+"\n")

Resources