Python 无法将单个str转换为Int

Python 无法将单个str转换为Int,python,python-3.x,Python,Python 3.x,在这个问题中,我通过每行拆分日志信息,然后将其附加到列表中来解析日志信息。然后,我通过提取时间戳和删除任何不是数字的东西来“清理”它。现在我尝试将每个数字(仍然是字符串)转换为整数类型 我仍然有一个错误,不知道我能做些什么来解决这个问题。我已添加包含以下时间戳信息的url: 网址: 不确定这是否是您正在寻找的,但您已经找到了: >>> from datetime import datetime >>> with open('messages.log', 'r'

在这个问题中,我通过每行拆分日志信息,然后将其附加到列表中来解析日志信息。然后,我通过提取时间戳和删除任何不是数字的东西来“清理”它。现在我尝试将每个数字(仍然是字符串)转换为整数类型

我仍然有一个错误,不知道我能做些什么来解决这个问题。我已添加包含以下时间戳信息的url:

网址:


不确定这是否是您正在寻找的,但您已经找到了:

>>> from datetime import datetime
>>> with open('messages.log', 'r') as f:
...     lines = f.readlines()
>>> for line in lines:
...     pieces = line.split(' ')
...     date_format = r'%Y-%m-%dT%H:%M:%S'
...     date = datetime.strptime(pieces[1], date_format)
...     print(date)
...
2014-07-03 23:24:31
2014-07-03 23:24:31
2014-07-03 23:24:31
2014-07-03 23:24:31

这两种解决方案需要发挥作用:

import os
import urllib.request

SHUTDOWN_EVENT = 'Shutdown initiated'

# prep: read in the logfile
logfile = #file that i have from the link
#deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)

with open(logfile) as f:
    loglines = f.readlines()

print(loglines)

def convert_to_datetime(line):

    #takes in the log, separates by line and makes a list per line.
    splitted_list = [i.split() for i in line]
    time_stamp_list = [i[1] for i in splitted_list]
    time_and_date = [i.split('T') for i in time_stamp_list]
    test_parse = [
        i.replace('-', ' ')
            .replace("T", " ")
            .replace(":", " ")
            .split(" ") for i in time_stamp_list
    ]

    print('test parse: ')
    print(test_parse)

    #This returns the necessary values into strings.  Now it's time to convert them into ints, and then a dictionary

    test_parse_num = []
    #return one list with all the nums:
    for k in test_parse:
        for i in k:
            test_parse_num.append(int(i))
    print(test_parse_num)
或:


1.请发布错误消息。2.为什么要将日期作为字符串而不是使用
datetime
对象?请显示示例预期结果
test\u parse\u num=[int(j)for i in test for j in i]
它属于2D列表类型。它可以工作,但正如@JaredSmith建议的那样,您必须使用datetime来处理此问题。
convert\u to\u datetime
调用在哪里?我需要找到一种方法,通过将整个字符串转换为数字列表来将此转换为日期时间。您能详细说明吗
date
是一个对象,它具有您可以想到的关于日期和时间的所有属性。就是它!非常感谢。我现在正试图将此信息传递到datetime函数中:final_prod=[datetime(*I)for I in test_parse_num]print(final_prod),但它返回为:[datetime.datetime(2014,7,3,23,24,31),datetime.datetime(2014,7,3,23,24,31),datetime.datetime(2014,7,3,23,24,31),…]
import os
import urllib.request

SHUTDOWN_EVENT = 'Shutdown initiated'

# prep: read in the logfile
logfile = #file that i have from the link
#deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)

with open(logfile) as f:
    loglines = f.readlines()

print(loglines)

def convert_to_datetime(line):

    #takes in the log, separates by line and makes a list per line.
    splitted_list = [i.split() for i in line]
    time_stamp_list = [i[1] for i in splitted_list]
    time_and_date = [i.split('T') for i in time_stamp_list]
    test_parse = [
        i.replace('-', ' ')
            .replace("T", " ")
            .replace(":", " ")
            .split(" ") for i in time_stamp_list
    ]

    print('test parse: ')
    print(test_parse)

    #This returns the necessary values into strings.  Now it's time to convert them into ints, and then a dictionary

    test_parse_num = []
    #return one list with all the nums:
    for k in test_parse:
        for i in k:
            test_parse_num.append(int(i))
    print(test_parse_num)
import os
import urllib.request

SHUTDOWN_EVENT = 'Shutdown initiated'

# prep: read in the logfile
logfile = #file that i have from the link
#deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)

with open(logfile) as f:
    loglines = f.readlines()

print(loglines)

def convert_to_datetime(line):

    #takes in the log, separates by line and makes a list per line.
    splitted_list = [i.split() for i in line]
    time_stamp_list = [i[1] for i in splitted_list]
    time_and_date = [i.split('T') for i in time_stamp_list]
    test_parse = [
        i.replace('-', ' ')
            .replace("T", " ")
            .replace(":", " ")
            .split(" ") for i in time_stamp_list
    ]

    print('test parse: ')
    print(test_parse)

    #This returns the necessary values into strings.  Now it's time to convert them into ints, and then a dictionary

    test_parse_num = []
    #returns a list with lists in it(a list for every log):
    for k in test_parse:
        new = []
        for i in k:

            new.append(int(i))
        test_parse_num.append(new)
    print(test_parse_num)