将数据字符串转换为列表
convert data string to list
我在处理一些输入时遇到了一些问题。
我正在从日志文件中读取数据并根据名称存储不同的值。
所以我的输入字符串由 ip、名称、时间和数据值组成。
日志行看起来像这样,它有 \t 间距:
134.51.239.54 Steven 2015-01-01 06:09:01 5423
我正在使用以下代码读取值:
loglines = file.splitlines()
data_fields = loglines[0] # IP NAME DATE DATA
for loglines in loglines[1:]:
items = loglines.split("\t")
ip = items[0]
name = items[1]
date = items[2]
data = items[3]
这很好用,但我需要将所有名称提取到一个列表中,但我还没有找到有效的解决方案。
当我使用打印名称时,我得到:
Steven
Max
Paul
我确实需要这样的姓名列表:
['Steven', 'Max', 'Paul',...]
可能有一个简单的解决方案,我还没有想出来,但是有人可以帮忙吗?
谢谢
只需创建一个空列表并在遍历文件时添加名称。
另请注意,如果该文件非常大,file.splitlines()
可能不是最好的主意,因为它将整个文件读入内存——然后您基本上 copy 所有这些都是通过做 loglines[1:]
。最好将 file
对象本身用作迭代器。并且不要使用 file
作为变量名,因为它会遮盖类型。
with open("some_file.log") as the_file:
data_fields = next(the_file) # consumes first line
all_the_names = [] # this will hold the names
for line in the_file: # loops over the rest
items = line.split("\t")
ip, name, date, data = items # you can put all this in one line
all_the_names.append(name) # add the name to the list of names
或者,您可以使用 zip
和 map
将其全部放入一个表达式中(使用 loglines
数据),但您不应该这样做... zip(*map(lambda s: s.split('\t'), loglines[1:]))[1]
我在处理一些输入时遇到了一些问题。 我正在从日志文件中读取数据并根据名称存储不同的值。 所以我的输入字符串由 ip、名称、时间和数据值组成。 日志行看起来像这样,它有 \t 间距:
134.51.239.54 Steven 2015-01-01 06:09:01 5423
我正在使用以下代码读取值:
loglines = file.splitlines()
data_fields = loglines[0] # IP NAME DATE DATA
for loglines in loglines[1:]:
items = loglines.split("\t")
ip = items[0]
name = items[1]
date = items[2]
data = items[3]
这很好用,但我需要将所有名称提取到一个列表中,但我还没有找到有效的解决方案。
当我使用打印名称时,我得到:
Steven
Max
Paul
我确实需要这样的姓名列表:
['Steven', 'Max', 'Paul',...]
可能有一个简单的解决方案,我还没有想出来,但是有人可以帮忙吗?
谢谢
只需创建一个空列表并在遍历文件时添加名称。
另请注意,如果该文件非常大,file.splitlines()
可能不是最好的主意,因为它将整个文件读入内存——然后您基本上 copy 所有这些都是通过做 loglines[1:]
。最好将 file
对象本身用作迭代器。并且不要使用 file
作为变量名,因为它会遮盖类型。
with open("some_file.log") as the_file:
data_fields = next(the_file) # consumes first line
all_the_names = [] # this will hold the names
for line in the_file: # loops over the rest
items = line.split("\t")
ip, name, date, data = items # you can put all this in one line
all_the_names.append(name) # add the name to the list of names
或者,您可以使用 zip
和 map
将其全部放入一个表达式中(使用 loglines
数据),但您不应该这样做... zip(*map(lambda s: s.split('\t'), loglines[1:]))[1]