Python 将多行放入数组

发布于 2024-12-09 18:46:38 字数 1221 浏览 0 评论 0原文

使用正则表达式我正在搜索包含数据的文本文件。我得到与此类似的输出。 例如,这是我得到的结果:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

氛圍 2024-12-16 18:46:38

您的数据已在列表中。如果您想以数组表示法打印出来,请将以下内容替换

for result in List:
    print(result)

为:

print List

您确实不应该将列表称为 List,尽管 - list 是保留字,并且List 非常相似。

顺便说一句,

List.append(str(first)+","+str(second)+"," \
               +str(third)+","+str(fourth)+"," \
               +str(bw)+","+str(value)+"," \
               +str(secondvalue)+","+str(hfs))

如果您使用其他一些 Python 功能(例如 join:),则 this: 会更容易理解:

List.append(",".join([first, second, third, fourth, bw, value, secondvalue, hfs]))

事实上,由于您的变量只是正则表达式中的组,因此您可以将整个事情缩短为:

List.append(",".join(m.groups()))

Your data is already in a list. If you want to print it out in array notation, replace this:

for result in List:
    print(result)

with this:

print List

You really shouldn't call your list List, though - list is a reserved word, and List is confusingly similar.

Incidentally, this:

List.append(str(first)+","+str(second)+"," \
               +str(third)+","+str(fourth)+"," \
               +str(bw)+","+str(value)+"," \
               +str(secondvalue)+","+str(hfs))

is much more comprehensible if you use some other Python features, like join:

List.append(",".join([first, second, third, fourth, bw, value, secondvalue, hfs]))

in fact, since your variables are just groups from the regular expression, you could shorten the whole thing to this:

List.append(",".join(m.groups()))
慵挽 2024-12-16 18:46:38

你尝试过吗:

print List

如果你想在一个字符串中:

result = str(List)

Have your tried:

print List

If you want that in a string:

result = str(List)
谜兔 2024-12-16 18:46:38

假设您拥有的是字符串:(

'"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'

不清楚如何使用正则表达式获取数据,因为您没有显示任何代码),请使用

[x.strip('"') for x in '"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'.split()]

来获取列表(不是数组,这是一个不同的事情):

['149', '149', '153', '153', '159', '159', '165', '165', '36', '36', '44']

如果您真正想要的是一个数组(它只能存储数值,而不是您所显示的数字的字符串表示形式,请使用):

import array
foo = array.array('i',(int(x.strip('"')) for x in '"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'.split()))

Assuming what you have is the string:

'"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'

(it's unclear how you're getting the data with a regex, as you've shown no code), use

[x.strip('"') for x in '"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'.split()]

to get the list (not array, which is a different thing):

['149', '149', '153', '153', '159', '159', '165', '165', '36', '36', '44']

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):

import array
foo = array.array('i',(int(x.strip('"')) for x in '"149" "149" "153" "153" "159" "159" "165" "165" "36" "36" "44"'.split()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文