首页 > 解决方案 > 无法将单个 str 转换为 Int

问题描述

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

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

网址:http ://projects.bobbelderbos.com/pcc/messages.log

import os
import urllib.request

SHUTDOWN_EVENT = 'Shutdown initiated'

# prep: read in the logfile
logfile = #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


    #this is where I am having trouble:
    test_parse_num = [int(i) for i in test_parse]
    print(test_parse_num)


标签: pythonpython-3.x

解决方案


这两个解决方案需要工作:

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)

推荐阅读