change String to datetime from an input file - python-3.x

I am getting an input file which contains datetime. My purpose is to read the file and filter the db.
Input file sample
2019-05-31 15:21:53
Below code work fine in my PC (python 3.6.4)
f=open("lastRun.txt", "r+")
lastHour=(f.read())
f.close()
lHourDate = datetime.datetime.strptime(lastHour, '%Y-%m-%d %H:%M:%S')
print(lHourDate)
however in production we have (Pyhton 3.6.7)
where the same code is throwing below error.
Traceback (most recent call last):
File "hourlyLoadEscenic.py", line 37, in <module>
lHourDate = datetime.strptime(lastHour, "%Y-%m-%d %H:%M:%S")
File "/usr/local/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/lib/python3.6/_strptime.py", line 365, in _strptime
data_string[found.end():])
ValueError: unconverted data remains:
I can do 'print' and see the lastHour has the input field. but in date conversion it is throwing error.
Please suggest

Related

Import Excel xlsx to Python using Panda - Error Message - How to resolve?

import pandas as pd
data = pd.read_excel (r'C:\Users\royli\Downloads\Product List.xlsx',sheet_name='Sheet1' )
df = pd.DataFrame(data, columns= ['Product'])
print (df)
Error Message
Traceback (most recent call last):
File "main.py", line 3, in <module>
Traceback (most recent call last):
File "main.py", line 3, in <module>
data = pd.read_excel (r'C:\Users\royli\Downloads\Product List.xlsx',sheet_name='Sheet1' )
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 353, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
return open_workbook(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/xlrd/__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\royli\\Downloads\\Product List.xlsx'

KeyboardInterrupt

Generally when I get that problem am gonna change \ symbols to \ \ symbols and generally its solved. Try it.
I had this problem in Visual Studio Code.
table = pd.read_excel('Sales.xlsx')
When running the program on Pycharm, there were no errors.
When trying to run the same program in Visual Studio Code, it showed an error, without any changes.
To fix it, I had to address the file with //. Ex:
table = pd.read_excel('C:\\Users\\paste\\Desktop\\archives\\Sales.xlsx')
I am using Pycharm and after reviewing the Post and replies, I was able to get this resolved (thanks very much). I didn't need to specify a worksheet, as there is only one sheet on the Excel file I am reading.
I had to add the r (raw string), and I also removed the drive specification c:
data = pd.read_excel(r'\folder\subfolder\filename.xlsx')

python3 cant convert string to datetime object

I have the following code:
from datetime import datetime
date_time_str = '2020-07-17 21:59:49.55'
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
print "The type of the date is now", type(date_time_obj)
print "The date is", date_time_obj
Which results in the err:
Traceback (most recent call last):
File "main.py", line 5, in <module>
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2020-07-17 21:59:49.553' does not match format '%y-%m-%d %H:%M:%S.%f'
Why cant I convert this date? The following example works:
date_time_str = '18/09/19 01:55:19'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
First of all, this is not valid Python 3 code. You've used the Python 2 print statement in your code, and trying to run this on Python 3 causes a SyntaxError.
As the error indicates, your date string does not match the format you specified. Take a look at the format codes; the first issue I notice is that you give a 4-digit year (2020) but try to line it up with %y, which is for two-digit year. There may be other issues as well, which should be easy to find looking through that table.

How to fix error 'Writing 0 cols but got 4 aliases' in python using pandas

I'm trying to save some data scraped using selenium to CSV file using pandas, but I'm getting this error and I don't know what's the problem.
I tried to use header=False instead of the header=['','','',''],
it works without error but gave me an empty CSV file.
Error:
full_datas = list(zip(d_links, d_title, d_price, d_num))
df = pd.DataFrame(data=full_datas)
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=['Links','Title,','Price','PNum'])
Empty csv:
full_datas = list(zip(d_links, d_title, d_price, d_num))
df = pd.DataFrame(data=full_datas)
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=False)
I'm expecting a CSV file with my data on it
Traceback (most recent call last):
File "E:/data/some/urls.py", line 90, in <module>
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=['Links','Title,','Price','PNum'])
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 3228, in to_csv
formatter.save()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 202, in save
self._save()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 310, in _save
self._save_header()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 242, in _save_header
"aliases".format(ncols=len(cols), nalias=len(header))
ValueError: Writing 0 cols but got 4 aliases
sometimes the list might be empty so i managed to fix it by using zip_longest
thank you guys for trying to help :)

Constant error when using ImageMagick with Python

When I did a code to convert PDF files to JPG images, but I have met an issue.Sometimes the .pdf file work well.But sometimes there is an error,the first temp file can't read.I checked the temp folder.When it works well, all the created temp file be deleted successfully. And when the error happens,only the first temp file is deleted. I can't resolve it.Anyone can fix?
code:
from wand.image import Image
with open("C:\software\1234.pdf",'rb') as f:
image_binary = f.read()
f.close()
with Image(blob=image_binary,resolution=400) as img:
Traceback (most recent call last):
File "C:\xxxx\eclipse-workspace\test\123\pdf2jpg.py", line 16, in
with Image(blob=image_binary,resolution=400) as img:
File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2742, in init
self.read(blob=blob, resolution=resolution)
File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2822, in read
self.raise_exception()
File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.CorruptImageError: unable to read image data `C:/xxxx/AppData/Local/Temp/magick-22200zyw89Zpq8IFJ1' # error/pnm.c/ReadPNMImage/1344
Exception ignored in:
Traceback (most recent call last):
File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\resource.py", line 232, in del
File "C:\xxxx\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 2767, in destroy
TypeError: object of type 'NoneType' has no len()

Convert string time stamp to seconds or milliseconds in python

I have a data frame with the entry Logs.loc[0,1])[0:18] outputs '13:51:32.006655755' and I would like to convert this to milliseconds.
How would one convert this into milliseconds. I was trying to use the following:
dt.datetime.strptime((Logs.loc[0,1])[0:18], '%H:%M:%S.%f')
Traceback (most recent call last):
File "", line 1, in
dt.datetime.strptime((Logs.loc[0,1])[0:18], '%H:%M:%S.%f')
File "C:\Program Files\Anaconda3\lib_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Program Files\Anaconda3\lib_strptime.py", line 346, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 755
use pd.to_timedelta and the total_seconds method
pd.to_timedelta(Logs.loc[0,1])[0:18]).total_seconds() * 1000
If you wanted to convert the entire column
pd.to_timedelta(Logs.iloc[:, 1].str[0:18]).dt.total_seconds() * 1000

Resources