以下是我的.log文件的内容
当前日期&时间,跨度,经过时间,响应时间,跨性别 率,
Wed Jul 18 10:03:1,5,3.7秒,0.00秒,13.51 t / s,
Wed Jul 18 10:03:5,5,4.5秒,0.00秒,11.11 t / s,
Wed Jul 18 10:04:0,5,0.91秒,0.00秒,5.49 t / s,
7月18日星期三10:22:4,12,0.79秒,0.00秒,15.19吨/秒,
7月18日星期三10:23:0,12,0.56秒,0.00秒,21.43吨/秒,
7月18日星期三10:23:1,12,0.53秒,0.00秒,22.64吨/秒,
我想将这些值直接放入python的2D列表中,但我找到的每个解决方案都只为每一行创建一个列表项。我希望列表槽的值是逗号分隔值。列表中的EG第3列第2列将存储5.它可能非常简单,但我一直在搜索和搜索,并没有发现这样做。任何帮助非常感谢。欢呼声。
更新
追踪(最近一次通话):
文件“StressTestCompare.py”,第45行,in
打印数据[2] [0]
IndexError:列表索引超出范围
[['当前日期&时间','\ t \ tTrans','\ tElapsed Time','\ tResponse Time','\ tTrans Rate','\ tThroughput','\ tConc','\ tTOKAY','\ tFOF','\ t数据传输','\ tIP地址',''],['7月18日星期三10:03:1','\ t5','\ t0.37秒','\ t0.00秒','\ t13.51 t / s','\ t0.00 b / s','\ t0.00','\ t0','\ t0','\ t0 bytes','\ t10.2.2.55:8080'], [],['Wed Jul 18 10:03:5','\ t5','\ t0.45 sec','\ t0.00秒','\ t11.11 t / s','\ t0。 00 b / s','\ t0.00','\ t0','\ t5','\ t0 bytes','\ t10.2.2.56:8080'],[],['Wed Jul 18 10 :04:0','\ t5','\ t0.91秒','\ t0.00秒','\ t5.49 t / s','\ t1609.89 b / s','\ t0 .00','\ t5','\ t0','\ t1465字节','\ t10.2.2.57:8080'],[],['Wed Jul 18 10:22:4','\ t12 ','\ t \ t0.79秒','\ t0.00秒','\ t15.19 t / s','\ t0.00 b / s','\ t0.00','\ t0', '\ t0','\ t0 bytes','\ t10.2.2.55:8080'],[],['Wed Jul 18 10:23:0','\ t12','\ t0.56 sec' ,'\ t0.00秒','\ t21.43 t / s','\ t0.00 b / s','\ t0.00','\ t0','\ t12','\ t0字节','\ t \ t10.2.2.56:8080'],[],['Wed Jul 18 10:23:1','\ t12','\ t0.53 sec','\ t0.00 sec', '\ t22.64 t / s','\ t0.00 b / s', '\ t0.00','\ t0','\ t0','\ t0 bytes','\ t10.2.2.57:8080'],[]]
使用print repr(数据)就像这样,是否有可能以更容易阅读的格式获取它?即使它仍然看起来就像我能看到的一样,所以我不知道为什么我会超出范围错误。
答案 0 :(得分:6)
csv模块可以派上用场:
import csv
with open('data.txt', 'rb') as inf:
data = list(csv.reader(inf, skipinitialspace=True))
data = [i for i in data if i] ## add to deal w/ blank lines in data file
会给你带变量data
的内容:
[['Current Date & Time',
'Trans',
'Elapsed Time',
'Response Time',
'Trans Rate',
''],
['Wed Jul 18 10:03:1', '5', '0.37 sec', '0.00 sec', '13.51 t/s', ''],
['Wed Jul 18 10:03:5', '5', '0.45 sec', '0.00 sec', '11.11 t/s', ''],
['Wed Jul 18 10:04:0', '5', '0.91 sec', '0.00 sec', '5.49 t/s', ''],
['Wed Jul 18 10:22:4', '12', '0.79 sec', '0.00 sec', '15.19 t/s', ''],
['Wed Jul 18 10:23:0', '12', '0.56 sec', '0.00 sec', '21.43 t/s', ''],
['Wed Jul 18 10:23:1', '12', '0.53 sec', '0.00 sec', '22.64 t/s', '']]
和data[2][1]
(不是data[3][2]
由于从零开始索引)会给你
'5'
一个字符串,您可以使用5
int(data[2][1])
<强>更新强>:
添加data = [i for i in data if i]
来处理输入中可能出现的空白行,从而导致OP更新后的帖子出现问题。
答案 1 :(得分:3)
查看pandas
:
>>> import pandas
>>> a = pandas.read_csv('input.csv')
>>> print a
Current Date & Time Trans Elapsed Time Response Time Trans Rate Unnamed: 5
0 Wed Jul 18 10:03:1 5 0.37 sec 0.00 sec 13.51 t/s NaN
1 Wed Jul 18 10:03:5 5 0.45 sec 0.00 sec 11.11 t/s NaN
2 Wed Jul 18 10:04:0 5 0.91 sec 0.00 sec 5.49 t/s NaN
3 Wed Jul 18 10:22:4 12 0.79 sec 0.00 sec 15.19 t/s NaN
4 Wed Jul 18 10:23:0 12 0.56 sec 0.00 sec 21.43 t/s NaN
5 Wed Jul 18 10:23:1 12 0.53 sec 0.00 sec 22.64 t/s NaN
>>> print a[' Trans'] * 2
0 10
1 10
2 10
3 24
4 24
5 24
Name: Trans