Python 将多行放入数组
使用正则表达式我正在搜索包含数据的文本文件。我得到与此类似的输出。 例如,这是我得到的结果:
36
37
36
36
36
76
39
36
68
36
56
36
36
36
...
我需要将所有 36 个都放入数组中,如下所示 [ '36', '36', .... ] 示例代码如下。
#!/usr/bin/python
import re
log = re.compile('Deleted file number: first: (\d+), second (\d+), third (\d+), fourth (\d+), bw (\d+), value: ([\dabcdefx]+), secondvalue: ([\d.]+)LM, hfs: ([\d.-]+)ls')
logfile = open("log.txt", "r").readlines()
List = []
for line in logfile:
m = log.match(line)
if m:
first = int (m.group(1))
second = int (m.group(2))
third = int (m.group(3))
fourth = int (m.group(4))
bw = int (m.group(5))
value = int (m.group(6),0)
secondvalue = float (m.group(7))
hfs = float (m.group(8))
List.append(str(first)+","+str(second)+"," \
+str(third)+","+str(fourth)+"," \
+str(bw)+","+str(value)+"," \
+str(secondvalue)+","+str(hfs))
for result in List:
print(result)
我可以使用 sys.stdout.write() 将其显示在与打印项目相同的一行中, 但是我怎样才能将所有这些放入一个数组中,就像 array = [ "149", 149", "153", "153" 等等]
任何帮助将不胜感激。
Using regexp I am searching through text file which contains data. I get output similar to this.
Here's what I get for example:
36
37
36
36
36
76
39
36
68
36
56
36
36
36
...
I need all those 36 to be in array like this [ '36', '36', .... ] The sample code is below.
#!/usr/bin/python
import re
log = re.compile('Deleted file number: first: (\d+), second (\d+), third (\d+), fourth (\d+), bw (\d+), value: ([\dabcdefx]+), secondvalue: ([\d.]+)LM, hfs: ([\d.-]+)ls')
logfile = open("log.txt", "r").readlines()
List = []
for line in logfile:
m = log.match(line)
if m:
first = int (m.group(1))
second = int (m.group(2))
third = int (m.group(3))
fourth = int (m.group(4))
bw = int (m.group(5))
value = int (m.group(6),0)
secondvalue = float (m.group(7))
hfs = float (m.group(8))
List.append(str(first)+","+str(second)+"," \
+str(third)+","+str(fourth)+"," \
+str(bw)+","+str(value)+"," \
+str(secondvalue)+","+str(hfs))
for result in List:
print(result)
I can use sys.stdout.write() to display it in one single line same with print item,
But how can I put all this into one array to be like array = [ "149", 149", "153", "153" and so on]
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的数据已在列表中。如果您想以数组表示法打印出来,请将以下内容替换
为:
您确实不应该将列表称为
List
,尽管 -list
是保留字,并且List
非常相似。顺便说一句,
如果您使用其他一些 Python 功能(例如 join:),则 this: 会更容易理解:
事实上,由于您的变量只是正则表达式中的组,因此您可以将整个事情缩短为:
Your data is already in a list. If you want to print it out in array notation, replace this:
with this:
You really shouldn't call your list
List
, though -list
is a reserved word, andList
is confusingly similar.Incidentally, this:
is much more comprehensible if you use some other Python features, like join:
in fact, since your variables are just groups from the regular expression, you could shorten the whole thing to this:
你尝试过吗:
如果你想在一个字符串中:
Have your tried:
If you want that in a string:
假设您拥有的是字符串:(
不清楚如何使用正则表达式获取数据,因为您没有显示任何代码),请使用
来获取列表(不是数组,这是一个不同的事情):
如果您真正想要的是一个数组(它只能存储数值,而不是您所显示的数字的字符串表示形式,请使用):
Assuming what you have is the string:
(it's unclear how you're getting the data with a regex, as you've shown no code), use
to get the list (not array, which is a different thing):
If what you really want is an array (which can only store numeric values, not string representations of numbers which is what you're showing, use):