开发者

Python put multiple lines to array

Using regexp I am searching thro开发者_开发技巧ugh 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.


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


Have your tried:

print List

If you want that in a string:

result = str(List)


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()))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜