Getting a datetime object from strptime - python-2.7

I'm trying to write a script that formats the date in a log file. The log file is meant to be read by excel after the conversion.
import datetime
def excel_date(date1):
temp = dt.datetime(1899, 12, 30)
delta = date1 - temp
return float(delta.days) + (float(delta.seconds) / 86400)
data=open(".\input.log").read()
file_ = open('output.log', 'w')
for row in data.split('\n'):
prefix = row[:1]
sdate = row[1:29]
suffix = row[30:]
offset = datetime.timedelta(0)
dt = datetime.datetime.strptime(sdate, '%a %b %d %H:%M:%S.%f %Y') + offset
excelDate = excel_date(dt)
file_.write(dt.strftime('%d.%m.%Y %H:%M:%S') + "\t" + excelDate + "\t"+ suffix + "\n")
file_.close()
The problem occurs when I try to use the excel_date function. I know that strptime returns a string and that the function expects a datetime object. Is there a way of creating a datetime object from strptime or converting the string into one?
Thanks

Related

How do I get today's date automatically as an integer in python

I want to write a code where it returns user's age when user enters his age in YYYYmmdd format. I have noticed that error lies in the line when I need to get the today's date as an integer but cannot convert strftime to an integer. I am using python 2.7 Any help? thanks!
import datetime
class User(object):
def __init__(self, full_name, birthday):
self.full_name = full_name
self.birthday = birthday
def calculate_age(self):
"""Return the age"""
yyyy = int(self.birthday[0:4])
mm = int(self.birthday[4:6])
dd = int(self.birthday[6:8])
dob = datetime.date(yyyy, mm, dd)
today = datetime.datetime.today().strftime('%Y%m%d')
today = int(today)
age_in_days = (today - dob)
age_in_years = age_in_days / 365
return int(age_in_years)
def main():
user_enter = raw_input("Enter your b'day:")
User1 = User("susa", user_enter)
print ("your age is" + User1.calculate_age())
if __name__ == "__main__":
main()
Instead of going to all the trouble of converting things to int, you can calculate the difference between two date objects and obtain the result's number of days:
...
dob = datetime.date(yyyy, mm, dd)
today = datetime.datetime.today().date()
age_in_days = (today - dob).days
...
Also, there as some extra things that you can consider reviewing in your code:
Your print is trying to concatenate a string with an int and it won't work.
You can cast the calculate_age() result to fix this:
print("your age is " + str(User1.calculate_age()))
You can use strptime to convert your birthday string input to datetime
and you can call date() to convert your datetime to date. This way you can avoid having to manually breaking your string into parts:
dob = datetime.datetime.strptime(self.birthday, '%Y%m%d').date()

Input query for python code

So I have created this code for my research, but I want to use it for plenty of data files, I do not want to do it manually, which means retyping some lines in my code to use desired file. How to use input command in python (I work with python 2.7 on Windows OS) to use it faster, just by typing name of desired datafile. My code so far:
import iodata as io
import matplotlib.pyplot as plt
import numpy as np
import time
from scipy.signal import welch
from scipy import signal
testInstance = io.InputConverter()
start = time.time()
conversionError = io.ConversionError()
#data = testInstance.convert(r"S:\Doktorat\Python\", 1", conversionError)
data = testInstance.convert(r"/Users/PycharmProjects/Hugo/20160401", "201604010000", conversionError)
end = time.time()
print("time elapsed " + str(end - start))
if(conversionError.conversionSucces):
print("Conversion succesful")
if(conversionError.conversionSucces == False):
print("Conversion failed: " + conversionError.conversionErrorLog)
print "Done!"
# Create a new subplot for two cannals 1 & 3
a = np.amin(data.data)
Bx = data.data[0,]
By = data.data[1,]
dt = float(300)/266350
Fs = 1/dt
t = np.arange(0,300,dt*1e3)
N = len(Bx)
M = len(By)
time = np.linspace(0,300,N)
time2 = np.linspace(0,300,M)
filename = 'C:/Users/PycharmProjects/Hugo/20160401/201604010000.dat'
d = open(filename,'rb')
degree = u"\u00b0"
headersize = 64
header = d.read(headersize)
ax1 = plt.subplot(211)
ax1.set_title(header[:16] + ', ' + # station name
'Canals: '+header[32:33]+' and '+header[34:35]+ ', ' # canals
+'Temp'+header[38:43]+degree+'C' # temperature
+', '+'Time:'+header[26:32]+', '+'Date'+' '+header[16:26]) # date
plt.ylabel('Pico Tesle [pT]')
plt.xlabel('Time [ms]')
plt.grid()
plt.plot(time[51:-14], Bx[51:-14], label='Canal 1', color='r', linewidth=0.1, linestyle="-")
plt.plot(time2[1:-14], By[1:-14], label='Canal 3', color='b', linewidth=0.1, linestyle="-")
plt.legend(loc='upper right', frameon=False, )
# Create a new subplot for FFT
plt.subplot(212)
plt.title('Fast Fourier Transform')
plt.ylabel('Power [a.u.]')
plt.xlabel('Frequency Hz')
xaxis2 = np.arange(0,470,10)
plt.xticks(xaxis2)
fft1 = (Bx[51:-14])
fft2 = (By[1:-14])
plt.grid()
# Loop for FFT data
for dataset in [fft1]:
dataset = np.asarray(dataset)
freqs, psd = welch(dataset, fs=266336/300, window='hamming', nperseg=8192)
plt.semilogy(freqs, psd/dataset.size**0, color='r')
for dataset2 in [fft2]:
dataset2 = np.asarray(dataset2)
freqs2, psd2 = welch(dataset2, fs=266336/300, window='hamming', nperseg=8192)
plt.semilogy(freqs2, psd2/dataset2.size**0, color='b')
plt.show()
As you can see there are some places where it would be better to put input and when I run the code I can write names of filenames etc. to python instead of creating every single pythonfile, with specified info in the code.
Btw. I use Pycharm to my python.
If all you are trying to do is get rid of the hardcoded pathname, you should be able to format your name string with input variables
name = raw_input("Name: ")
measurement = raw_input("Measurement: ")
filename = "C:/Users/PycharmProjects/{0}/{1}".format(name, measurement)
see raw_input and string formatting

the datetime strftime() methods require year >= 1900

I have the date of 1515 in my data set column 4, is there any way to bypass this error?
cols[4] = cols[4] and cols[4].strftime("%d/%m/%Y") or ""
ValueError: year=1515 is before 1900; the datetime strftime() methods require year >= 1900
Here is the exact code:
cursor.execute(SQL)
filename = r"C:\Projects\OPEN_KCI3.csv"
with open(filename, "wb") as fout:
writer = csv.writer(fout)
#writer.writerow([i[0] for i in cursor.description ]) # heading row
for row in cursor.fetchall():
cols = list(row)
cols[3] = cols[3] and cols[3].strftime("%d/%m/%Y") or ""
cols[4] = cols[4] and cols[4].strftime("%d/%m/%Y") or ""
writer.writerow(cols)
cursor.close()
connection.close()

Python, WindowsError: [Error 32], file being used by another process

I'm writing a small programm that's supposed to loop through a folder of msg files (i.e. MS Outlook emails) and prefix a short string to their file names. I keep running into WindowsError: [Error 32] (The process cannot access the file because it is being used by another process) on line 33 (os.rename(filenameOLD, filenameNEW)). Any idea why?
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
import os
path = 'C:\Users\MyName\Desktop\SomeFile\\'
msgFiles = os.listdir(path) # returns list
count = 0
for msgFile in msgFiles:
count = count + 1
msg = outlook.OpenSharedItem(path + msgFile)
date = str(msg.SentOn)
#Extract YYYY, MM, DD, HHMMSS from .msg sent date
YYYY = str(20)+date.split("/")[2][:2]
MM = date.split("/")[1]
DD = date.split("/")[0]
HHMMSS = "".join(date.split()[1].split(":")) + "Hrs"
#Reformat date to valid file name
filenamePrefix = YYYY + DD + MM + " " + HHMMSS + " "
#generate new file name
filenameOLD = path + msgFile
filenameNEW = path + filenamePrefix + msgFile
#rename file
os.rename(filenameOLD, filenameNEW)
print count, "files renamed"
You've opened the message without closing it. Instead do:
# ...
for msgFile in msgFiles:
count = count + 1
msg = outlook.OpenSharedItem(path + msgFile)
date = str(msg.SentOn)
del msg
# ...

XLRDError: No sheet named <'Sheet1'> in python

I am trying to convert the xls into csv file using pandas in python. But I am getting the following error like 'XLRDError: No sheet named <'Sheet1'>'. I have verified the sheet name and it is same as specified above, but I don't how to correct this error. Please find my code below.
CODE:
def xls_2_csv():
import pandas as pd
data_xls = pd.read_excel(r'c:\delivery\file1.xls','Sheet1', index_col=None)
data_xls.to_csv(r'C:\test\file1.csv', encoding='utf-8',index=None)
xls_2_csv()
Please help me in solving this error. Thanks in advance.
I found the same problem in python 3.6 and pandas version is 0.25.1.
The following should work:
import pandas as pd
file = 'your excel file path'
# the file is endswith '.xls' and there is multiple sheets
# error method
df_sheet1 = pd.read_excel(file, sheet_name='Sheet1')
df_sheet2 = pd.read_excel(file, sheet_name='Sheet2')
# when read Sheet1 had no error, but when read Sheet2, had an error:
# xlrd.biffh.XLRDError: No sheet named <'Sheet2'>
# right method
with pd.ExcelFile(file) as xls:
for sheet_name in xls.sheet_names:
df = pd.read_excel(xls, sheet_name=sheet_name)
print(df.head())
Hi I tried the following code it worked for me.
CODE:
import logging
import time
import traceback
import xlrd
import csv
import sys
import re
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
xls = input file path
target = output file path
logging.info("Start converting: From '" + xls + "' to '" + target + "'. ")
try:
start_time = time.time()
wb = xlrd.open_workbook(xls)
sh = wb.sheet_by_index(0)
csvFile = open(target, 'wb')
wr = csv.writer(csvFile, quoting=csv.QUOTE_ALL)
for row in xrange(sh.nrows):
rowValues = sh.row_values(row)
newValues = []
for s in rowValues:
if isinstance(s, unicode):
strValue = (str(s.encode("utf-8")))
else:
strValue = (str(s))
isInt = bool(re.match("^([0-9]+)\.0$", strValue))
if isInt:
strValue = int(float(strValue))
else:
isFloat = bool(re.match("^([0-9]+)\.([0-9]+)$", strValue))
isLong = bool(re.match("^([0-9]+)\.([0-9]+)e\+([0-9]+)$", strValue))
if isFloat:
strValue = float(strValue)
if isLong:
strValue = int(float(strValue))
newValues.append(strValue)
wr.writerow(newValues)
csvFile.close()
logging.info("Finished in %s seconds", time.time() - start_time)
except Exception as e:
print (str(e) + " " + traceback.format_exc())