Oh man, I'm sure you guys are going to have a good time making fun of my newbie self, so enjoy!

I have output that looks like this:

I take this output and I feed it to a function that I wrote:

def parseline(input,start,stop):
for line in input.readlines():
if ":" in line:
line = line[start:stop]
print line
It then spits out exactly what I want:

6.87
6.86
etc...all on separate lines, I only wanted the ones, tenths, and hundredths btw...

When it's done, I'm stuck with the list. The list is comprised of a bunch of numbers on differernt lines.

What I really need to do though is take this list and put it into an array and sort them from the "lowest" to the "highest" number.

When I try and put this list of strings into an array by using split, it doesn't work. I'm just stumped. Anyone know how to take the readlines() blob and split it up into an array?

-Link

Recommended Answers

All 2 Replies

Anyone know how to take the readlines() blob and split it up into an array?

The function readlines() already gives you an "array" (it's list in Python). So I guess that part's solved

Welcome to the forum Linky. Please, in the future use code tags (or any other appropriate tag) when you're posting in the forums. It helps us all read your posts better and therefore we're able to give you more solid help and quicker!

This will give you pretty output like this (this is your code reposted w/ tags):

def parseline(input,start,stop):
    for line in input.readlines(): 
        if ":" in line:
            line = line[start:stop]
            print line

--
Now on to your problem: if you're looking to store those numbers instead of merely print them you can just start with an empty list at the beginning of your function, and then append each value to it, which you can then pass back to the calling code:

def parseline(input,start,stop):
    my_numbers = []
    for line in input.readlines(): 
        if ":" in line:
            line = line[start:stop]
            my_numbers.append(float(line))
    return my_numbers

Then in your code when you call parseline(x,y,z) simply use something like numbers = parseline(x,y,z) instead. Then all you need to do is use numbers.sort()

EDIT: Also note that in your function I converted the number to a floating point with float()

EDIT 2: It's pretty common practice in Python to use 4 spaces instead of a tab. Most editors can be set to use a 4 space tabstop instead of a "tab character". The reason behind this is that different platforms can interpret a tab character to either be 4 or 8 spaces (or really anything that the user sets it to). The PEP-8 Coding Style recommends 4 spaces as the standard (it especially helps in this limited width forum so that your code isn't spanning multiple lines, which can become ugly). Here's the above code reposted with 4 space tabs:

def parseline(input,start,stop):
    my_numbers = []
    for line in input.readlines(): 
        if ":" in line:
            line = line[start:stop]
            my_numbers.append(float(line))
    return my_numbers

HTH

That works quite well and thank you. I didn't realize that it was already in an array (the "[") at the beggining and end of the string should have tipped me off!

I will definately enable Code Tags in all future threads and tell Crimson to stop tabbing and start 4-spacing.

-Regards, Link

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.