Date conversion python [duplicate] - python

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 6 years ago.
I have strings that look like this: "16-Jul-8"
I want to convert them to something like: 16/07/08
datestr = datefields[0]+"-"+datefields[1]+"-"+datefields[2]
dt = datetime.datetime(datestr)
dt.strftime('%d-%m-%y')
I keep coming up empty...

it's so easy,
from datetime import datetime
function = lambda date_str: datetime.strptime(date_str, "%y-%b-%d").strftime("%y/%m/%d")
print function("16-Jul-8")
You can read python's documentations from here.

Related

Convert '2019-10-16' do date object [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 3 years ago.
I realize this might be the most well-documented thing on the Internet but I can't seem to get it right. I have a string, '2019-10-16' that I want to turn into a date object so I can increase it incrementally, but can still be converted to the string '2019-10-06' again. However, I seem to only be able to get it as 2019.10.16 or something similar.
import datetime
day = '2019-10-16'
date_object = datetime.datetime.strptime(day, '%Y-%m-%d')
>date_object
>datetime.datetime(2019, 10, 16, 0, 0)
To change it use date_object.strftime('%Y-%m-%d')

Python: How to convert float to string without `.0`? [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 6 years ago.
I want to extract telephone number from *.xls with module xlrd using Python, but it shows like this:'16753435903.0'.
Here's the code:
opensheet = openxls.sheet_by_name(u'sms')
sheetrows = opensheet.nrows
for rownum in range(1, sheetrows):
rowvalue = opensheet.row(rownum)
execinfo = ""
for colnum in range(1,5):
execinfo += "'"+str(rowvalue[colnum].value)+"',"
The key part:
str(rowvalue[colnum].value)
How can I get the well format telephone number without .0?
This might be a very simple workaround: convert the number to an int before converting it to a string?

How to convert str to hex via python [duplicate]

This question already has answers here:
Print a string as hexadecimal bytes
(13 answers)
Closed 7 years ago.
I want to convert string to a hex (decimal),
for example abc to 616263 and so on.
How do I do that?
I have tried:
x= input("Enter string")
x = int(x)
hex(x)
But it is not working.
maybe you are looking for something like:
x.encode("hex")

Python Datetime Formatted as "1/1/1990" [duplicate]

This question already has answers here:
Python strftime - date without leading 0?
(21 answers)
How to convert a time to a string
(4 answers)
Closed 9 years ago.
In python how would I format the date as 1/1/1990?
dayToday = datetime.date(1990,1,1)
print dayToday
This returns 1990-01-01, but I want it to look like 1/1/1990. (Jan 1 1990)
Try to look into python datetime.strftime
dayToday = datetime.date(1990,1,1)
print dayToday.strftime('%Y/%m/%d')
>>> 1990/01/01
print dayToday.strftime('%Y/%b/%d')
>>> 1990/Jan/01
Use the datetime.strftime function with an appropriate format string:
>>> now = datetime.datetime.now()
>>> print now.strftime('%Y/%m/%d')
2013/04/19
Others have showed how to get the output 1990/01/01, but assuming you don't want the leading zeros in there, the only way that I know of to do it is to do the string formatting yourself:
>>> '{dt.year}/{dt.month}/{dt.day}'.format(dt = dt.datetime.now())
'2013/4/19'
With the correct format and a without leading 0:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%-m/%-d/%Y")
'4/19/2013'
Reported to only work for Linux, but I haven't tested anything else personally.
Tested and working for 2.7.3 and 3.2.3 on Linux x64.

Python: How to convert datetime format? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert a time to a string
I have a variable as shown in the below code.
a = "2011-06-09"
Using python, how to convert it to the following format?
"Jun 09,2011"
>>> import datetime
>>> d = datetime.datetime.strptime('2011-06-09', '%Y-%m-%d')
>>> d.strftime('%b %d,%Y')
'Jun 09,2011'
In pre-2.5 Python, you can replace datetime.strptime with time.strptime, like so (untested): datetime.datetime(*(time.strptime('2011-06-09', '%Y-%m-%d')[0:6]))
#Tim's answer only does half the work -- that gets it into a datetime.datetime object.
To get it into the string format you require, you use datetime.strftime:
print(datetime.strftime('%b %d,%Y'))

Categories