1

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.

4
  • 2
    A relevant excerpt from your code would help us. You can create a list and append each value to it. Commented Oct 13, 2011 at 11:12
  • I getting not string, but columns Commented Oct 13, 2011 at 11:20
  • Why are you converting them into int and float when you save them as string in the array. An array could be made simply by [first, second, third, ...] Commented Oct 13, 2011 at 11:21
  • Instead of List.append(str(first)...) do List.extend([first, second..]). Commented Oct 13, 2011 at 11:23

3 Answers 3

5

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()))
Sign up to request clarification or add additional context in comments.

Comments

1

Have your tried:

print List

If you want that in a string:

result = str(List)

2 Comments

I agree with @Nick Johnson about the naming issue.
And upvoted his answer because his groups suggestion will make your code much easier to read.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.