python帮助分隔文本文件中的列表

时间:2015-07-05 16:56:48

标签: python list

我有一个文本文件,其中有数百个列表存储在文本文件中。我如何分离列表然后将它们存储为列表并搜索列表之间的最小秒值。我对解决这个问题的任何新方法持开放态度。这是前几个'列表'我试图将它们分开

var line1=[["Apr 02 2014 01: +0",0.6,"295"],["Apr 03 2014 01: +0",0.641,"245"],["Apr 04 2014 01: +0",0.625,"246"],["Apr 05 2014 01: +0",0.665,"267"],["Apr 06 2014 01: +0",0.632,"226"],["Apr 07 2014 01: +0",0.672,"170"],["Apr 08 2014 01: +0",0.655,"147"],["Apr 09 2014 01: +0",0.654,"121"],["Apr 10 2014 01: +0",0.62,"136"],["Apr 11 2014 01: +0",0.629,"176"],["Apr 12 2014 01: +0",0.68,"190"],["Apr 13 2014 01: +0",0.677,"176"],["Apr 14 2014 01: +0",0.73,"153"],["Apr 15 2014 01: +0",0.587,"148"],["Apr 16 2014 01: +0",0.591,"134"],["Apr 17 2014 01: +0",0.612,"148"],["Apr 18 2014 01: +0",0.593,"142"],["Apr 19 2014 01: +0",0.612,"153"],["Apr 20 2014 01: +0",0.654,"203"],["Apr 21 2014 01: +0",0.713,"156"],["Apr 22 2014 01: +0",0.711,"153"],["Apr 23 2014 01: +0",0.625,"128"],["Apr 24 2014 01: +0",0.629,"122"],["Apr 25 2014 01: +0",0.603,"139"],["Apr 26 2014 01: +0",0.6,"169"],["Apr 27 2014 01: +0",0.589,"177"],["Apr 28 2014 01: +0",0.585,"132"],["Apr 29 2014 01: +0",0.612,"120"],["Apr 30 2014 01: +0",0.626,"116"],["May 01 2014 01: +0",0.57,"142"]

我尝试将它们分开

with open('test.txt','r') as csvfile:
  writer=csv.reader(csvfile,delimeter=' , ',quotechar=csv.QUOTE_MINIMAL)
    for row in writer:
      print ','.join(row)

3 个答案:

答案 0 :(得分:3)

好的,假设这是格式方面的代表性输入:

var line1=[["Apr 02 2014 01: +0",0.6,"295"],["Apr 03 2014 01: +0",0.641,"245"]];

然后你可以做到以下几点:

import json

with open('test.txt', 'r') as datafile:
    data = datafile.read()

json_str = data.split('=', 1)[1].rstrip(';\n\r ')
my_data = json.loads(json_str)

for row in my_data:
    print row

print "Minimum by second value"
print min(my_data, key=lambda x: x[1])

将打印:

[u'Apr 02 2014 01: +0', 0.6, u'295']
[u'Apr 03 2014 01: +0', 0.641, u'245']
Minimum by second value
[u'Apr 02 2014 01: +0', 0.6, u'295']

答案 1 :(得分:1)

您可以使用re.finditer()在生成器中获取预期的列表。这是一种节省内存的优化方法。

然后,您可以使用ast.literal_eval将字符串转换为列表对象:

import ast
import re
with open(filename) as f:
     all_list=re.finditer(r'\[([^[\]]*?)\]',f)

演示:

print [ast.literal_eval(i.group()) for i in all_list]

[['Apr 02 2014 01: +0', 0.6, '295'], ['Apr 03 2014 01: +0', 0.641, '245'], ['Apr 04 2014 01: +0', 0.625, '246'], ['Apr 05 2014 01: +0', 0.665, '267'], ['Apr 06 2014 01: +0', 0.632, '226'], ['Apr 07 2014 01: +0', 0.672, '170'], ['Apr 08 2014 01: +0', 0.655, '147'], ['Apr 09 2014 01: +0', 0.654, '121'], ['Apr 10 2014 01: +0', 0.62, '136'], ['Apr 11 2014 01: +0', 0.629, '176'], ['Apr 12 2014 01: +0', 0.68, '190'], ['Apr 13 2014 01: +0', 0.677, '176'], ['Apr 14 2014 01: +0', 0.73, '153'], ['Apr 15 2014 01: +0', 0.587, '148'], ['Apr 16 2014 01: +0', 0.591, '134'], ['Apr 17 2014 01: +0', 0.612, '148'], ['Apr 18 2014 01: +0', 0.593, '142'], ['Apr 19 2014 01: +0', 0.612, '153'], ['Apr 20 2014 01: +0', 0.654, '203'], ['Apr 21 2014 01: +0', 0.713, '156'], ['Apr 22 2014 01: +0', 0.711, '153'], ['Apr 23 2014 01: +0', 0.625, '128'], ['Apr 24 2014 01: +0', 0.629, '122'], ['Apr 25 2014 01: +0', 0.603, '139'], ['Apr 26 2014 01: +0', 0.6, '169'], ['Apr 27 2014 01: +0', 0.589, '177'], ['Apr 28 2014 01: +0', 0.585, '132'], ['Apr 29 2014 01: +0', 0.612, '120'], ['Apr 30 2014 01: +0', 0.626, '116'], ['May 01 2014 01: +0', 0.57, '142']]

请注意,您可以处理每次迭代中的每个列表,并且不会被迫一次获取所有列表。

for i in all_list:
   # do stuff with ast.literal_eval(i.group())

答案 2 :(得分:0)

只要数据不太长(因此它适合内存),这些行中的某些内容应该有效。

import json

filename = "D:\\tmp\\test.txt"


final_lists = []

with open(filename, "r") as fl:
    #Read everything
    content = fl.read()

#Now split first line to keep only the lists
valid_part = content.split("=", 1)[1]
#Load valid part into json
cur_lists = json.loads(valid_part)

#Now find min value
_min = min(cur_lists, key=lambda x:x[1])