2

I have a str that contains a list of numbers and I want to convert it to a list. Right now I can only get the entire list in the 0th entry of the list, but I want each number to be an element of a list. Does anyone know of an easy way to do this in Python?

for i in in_data.splitlines():
    print i.split('Counter32: ')[1].strip().split()

my result not i want

['12576810']\n['1917472404']\n['3104185795']

my data

IF-MIB::ifInOctets.1 = Counter32: 12576810
IF-MIB::ifInOctets.2 = Counter32: 1917472404
IF-MIB::ifInOctets.3 = Counter32: 3104185795

i want result

['12576810','1917472404','3104185795']
2
  • 3
    how does your initial string look like? Commented May 4, 2012 at 13:13
  • 1
    It depends on the layout of the string. if it's just "4 8 15 16 23 42", then making it into a list is easy. If there's a lot of extra junk in the string, then it is harder. Commented May 4, 2012 at 13:13

6 Answers 6

8

Given your data as

>>> data="""IF-MIB::ifInOctets.1 = Counter32: 12576810
IF-MIB::ifInOctets.2 = Counter32: 1917472404
IF-MIB::ifInOctets.3 = Counter32: 3104185795"""

You can use regex where the intent is more clear

>>> import re
>>> [re.findall("\d+$",e)[0] for e in data.splitlines()]
['12576810', '1917472404', '3104185795']

or as @jamylak as pointed out

re.findall("\d+$",data,re.MULTILINE)

Or str.rsplit which will have a edge on performance

>>> [e.rsplit()[-1] for e in data.splitlines()]
['12576810', '1917472404', '3104185795']
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of using data.splitlines() for the regex version you can do. re.findall("\d+$",data,re.MULTILINE)
@jamylak: Aha nice, I forgot. THanks
data Each line have '\n', Your answer is not I ultimately want.
5

You are already quite far. Based on the code you have, try this:

result = []
for i in in_data.splitlines():
    result.append(i.split('Counter32: ')[1].strip())
print result

you could also do:

result = [i.split('Counter32: ')[1].strip() for i in in_data.splitlines()]

Then, you can also go and look at what @Abhijit and @KurzedMetal are doing with regular expressions. In general, that would be the way to go, but I really like how you avoided them with a simple split.

1 Comment

Unmatched parentheses in line 3.
2

My best try with the info you gave:

>>> data = r"['12576810']\n['1917472404']\n['3104185795']"
>>> import re
>>> re.findall("\d+", data)
['12576810', '1917472404', '3104185795']

you could even convert it to int or long if necesary with map()

>>> map(int, re.findall("\d+", data))
[12576810, 1917472404, 3104185795L]
>>> map(long, re.findall("\d+", data))
[12576810L, 1917472404L, 3104185795L]

Comments

2

This is how I'd do it.

data="""IF-MIB::ifInOctets.1 = Counter32: 12576810 ... IF-MIB::ifInOctets.2 = Counter32: 1917472404 ... IF-MIB::ifInOctets.3
= Counter32: 3104185795"""

[ x.split()[-1] for x in data.split("\n") ]

Comments

1
with open('in.txt') as f:
 numbers=[y.split()[-1] for y in f]
print(numbers)      

  ['12576810', '1917472404', '3104185795']

or:

with open('in.txt') as f:
    numbers=[]
    for x in f:
        x=x.split()
        numbers.append(x[-1])
print(numbers)  

['12576810', '1917472404', '3104185795']

1 Comment

.split() removes whitespace at the start and end of string. The .rstrip() call is redundant.
0
result = [(item[(item.rfind(' ')):]).strip() for item in list_of_data]

A variant using list comprehension. Iterator over all line of data, find the last index of a blank, cut down the strip from last found blank position to it's end, strip the resulting string (erase possible blanks) and put the result in a new list.

    data = """F-MIB::ifInOctets.1 = Counter32: 12576810
IF-MIB::ifInOctets.2 = Counter32: 1917472404
IF-MIB::ifInOctets.3 = Counter32: 3104185795"""
    result = [ (item[(item.rfind(' ')):]).strip() for item in data.splitlines()]
    print result

Result:

['12576810', '1917472404', '3104185795']

2 Comments

in_error,in_data = commands.getstatusoutput(in_cmd) if in_error != 0: print "Critical - cmd is error!! pls check!!!" else: for e in in_data.splitlines(): print e.rsplit()[-1]
I didn't specify the input and wrote 'list_of_data' instead, that's right. It's not the best solution, I see that now, but it's short and works.

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.