将字符串中的 strptime 值格式化为日期
Formatting a strptime value from a string as a date
我有以下代码,它是读取文本文件的开始,将其转换为 json 对象,然后获取其中的一些值并处理读入的日期字符串:
import json
from datetime import date, timedelta, datetime
my_list = []
my_list2 = []
filepath = 'myfile'
x_contents = open(filepath, "r")
x_contents = x_contents.read()
x_contents = json.loads(x_contents.replace("u'", '"').replace("'", '"'))
for mydate in x_contents:
for mydate2 in mydate:
the_match = mydate2[0]
the_date = mydate2[2]
the_date = datetime.strptime(the_date,"%A, %b %d %Y")
print the_date #writes to the log in the correct format
my_list.append(the_match)
my_list.append(the_date) #appends the strptime tuple to the list, not the formatted date as it appears with the print statement above
my_list2.append(my_list)
my_list = []
for i in my_list2:
print i
一些示例输出:
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
...
...
...
[958431, datetime.datetime(2015, 8, 8, 0, 0)]
[958427, datetime.datetime(2015, 8, 8, 0, 0)]
[958429, datetime.datetime(2015, 8, 8, 0, 0)]
[958430, datetime.datetime(2015, 8, 8, 0, 0)]
[958433, datetime.datetime(2015, 8, 8, 0, 0)]
...
...
...
任何人都可以告诉我在将列表的第二个元素转换为正确格式时我遗漏了什么吗?
谢谢
我猜 '2015-08-08 00:00:00'
就是你所说的 "correct format"。如果是:
>>> import datetime
>>> t = datetime.datetime(2015, 8, 8, 0, 0)
>>> t
datetime.datetime(2015, 8, 8, 0, 0)
>>> str(t)
'2015-08-08 00:00:00'
作为容器中的元素打印的对象是通过调用其 __repr__
而不是 __str__
来打印的,这就是为什么您的 datetime
对象似乎没有格式化为一个列表。看到这个:
>>> repr(t)
'datetime.datetime(2015, 8, 8, 0, 0)'
>>> [t, str(t)]
[datetime.datetime(2015, 8, 8, 0, 0), '2015-08-08 00:00:00']
编辑:
现在给出你的列表打印代码,其实很简单。只需将其更改为:
for i in my_list2:
# instead of i, print a list in which every item in i is converted to str(i)
print [str(item) for item in i]
或等效地:
for i in my_list2:
# map(str, i) does the same thing as [str(item) for item in i]
print map(str, i) # would have to be list(map(str, i)) in python 3
我认为你应该使用
datetime.datetime.strftime(the_date, "%x %X")
(或其他一些格式字符串)以您想要的方式格式化日期时间。查看 strftime 以获得更多指导。
我有以下代码,它是读取文本文件的开始,将其转换为 json 对象,然后获取其中的一些值并处理读入的日期字符串:
import json
from datetime import date, timedelta, datetime
my_list = []
my_list2 = []
filepath = 'myfile'
x_contents = open(filepath, "r")
x_contents = x_contents.read()
x_contents = json.loads(x_contents.replace("u'", '"').replace("'", '"'))
for mydate in x_contents:
for mydate2 in mydate:
the_match = mydate2[0]
the_date = mydate2[2]
the_date = datetime.strptime(the_date,"%A, %b %d %Y")
print the_date #writes to the log in the correct format
my_list.append(the_match)
my_list.append(the_date) #appends the strptime tuple to the list, not the formatted date as it appears with the print statement above
my_list2.append(my_list)
my_list = []
for i in my_list2:
print i
一些示例输出:
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
2015-08-08 00:00:00
...
...
...
[958431, datetime.datetime(2015, 8, 8, 0, 0)]
[958427, datetime.datetime(2015, 8, 8, 0, 0)]
[958429, datetime.datetime(2015, 8, 8, 0, 0)]
[958430, datetime.datetime(2015, 8, 8, 0, 0)]
[958433, datetime.datetime(2015, 8, 8, 0, 0)]
...
...
...
任何人都可以告诉我在将列表的第二个元素转换为正确格式时我遗漏了什么吗?
谢谢
我猜 '2015-08-08 00:00:00'
就是你所说的 "correct format"。如果是:
>>> import datetime
>>> t = datetime.datetime(2015, 8, 8, 0, 0)
>>> t
datetime.datetime(2015, 8, 8, 0, 0)
>>> str(t)
'2015-08-08 00:00:00'
作为容器中的元素打印的对象是通过调用其 __repr__
而不是 __str__
来打印的,这就是为什么您的 datetime
对象似乎没有格式化为一个列表。看到这个:
>>> repr(t)
'datetime.datetime(2015, 8, 8, 0, 0)'
>>> [t, str(t)]
[datetime.datetime(2015, 8, 8, 0, 0), '2015-08-08 00:00:00']
编辑:
现在给出你的列表打印代码,其实很简单。只需将其更改为:
for i in my_list2:
# instead of i, print a list in which every item in i is converted to str(i)
print [str(item) for item in i]
或等效地:
for i in my_list2:
# map(str, i) does the same thing as [str(item) for item in i]
print map(str, i) # would have to be list(map(str, i)) in python 3
我认为你应该使用
datetime.datetime.strftime(the_date, "%x %X")
(或其他一些格式字符串)以您想要的方式格式化日期时间。查看 strftime 以获得更多指导。