python - 无法将单个 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)
解决方案
这两个解决方案需要工作:
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)
推荐阅读
- vue.js - nuxt 查询字符串参数的文件夹/文件结构
- javascript - How to make every word in a sentence start with a capital letter
- flutter - 如何从 Flutter 中的另一个页面/类向 ListView 添加数据
- php - 我需要帮助
- python - Python:根据条件匹配列
- python - 对 sklearn Pipeline Imputer 的列顺序感到困惑
- r - 使用 r 中的日期变量获取最近的观察结果
- c# - 如何在 C# 中使用 IHTTPClientFactory
- r - 为什么在 R 中使用 plotcounts 和 ggplot 绘制时,我的一个组织样本的 DEG 是单独绘制的?
- android - 知道应用程序内部的信息作为另一个类中的列表存在,我如何在颤振中使用搜索