-2

I need to get the last part of a list that I have fetched with readlines(). This is a part of the string / list:

['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n', # and so on....]

I solely want to get the numbers and ignore the rest of them, but how do I do that? So I want this:

2.0 2.0 1.3

To get the name part, I know I need to use split("_") and use an index to fetch it...

When I try getting the numbers, though, I fail, because it just outputs nothing in the console.

This is the code that gets the names:

def openFile():
    fileFolder = open('TEXTFILE', 'r')
    readFile = fileFolder.readlines()

    for line in readFile:
        line = line.split("_")

        personNames = line[0]

        print personNames

print openFile()

I thought that using line[2] or line[3] would be sufficient to get the numbers as well, but it isn't. Can someone explain to me why that is not working and how I could get it to work, while using my style of code instead of importing stuff?

Is there something like a range or something that could say to split() just to get the last part of it?

Siyah
  • 2,852
  • 5
  • 37
  • 63
  • 1
    Index by `[-1]` to get the last element of the list. – EL_DON Nov 16 '16 at 20:25
  • In what way does the posted code even attempt to extract the numbers? – Scott Hunter Nov 16 '16 at 20:25
  • @ScottHunter You're right, but if it doesn't even print names, he has a problem he'll have to solve anyway before getting to the numbers. – EL_DON Nov 16 '16 at 20:26
  • @ScottHunter: it doesn't, because I don't know how I should do that. At Kevin: typo, sorry, fixed it. – Siyah Nov 16 '16 at 20:26
  • Is the code you're showing us here the same code you're using to try to get the numbers, and which outputs nothing in the console? At the very least, I'd expect this to print `None`, even if TEXTFILE has zero lines. – Kevin Nov 16 '16 at 20:27
  • It does print a None, indeed... This code just gives me the names, instead of the numbers. I don't know to fetch the numbers, that's my question. – Siyah Nov 16 '16 at 20:27
  • Thanks for bearing with my questions :-) What kind of output do you get if you do `print line[1]` inside the loop? – Kevin Nov 16 '16 at 20:28
  • 1
    Possible duplicate of [Python: Extract numbers from a string](http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string) – John Smith Nov 16 '16 at 20:29
  • @Kevin: it gives me spaces between the items... – Siyah Nov 16 '16 at 20:30
  • just read the duplicate, here is the answer in one of the posts `[re.findall("[-+]?\d+[\.]?\d*[eE]?[-+]?\d*", s) for s in l]` that works in your case – John Smith Nov 16 '16 at 20:34
  • @JohnSmith: yes, but I said to use "my style of code, instead of importing stuff". So that is not an answer to my question. – Siyah Nov 16 '16 at 20:37

3 Answers3

2

Based on your last question, you haven't gotten rid of those empty strings in this solution yet. So line[2] and line[3] didn't work because they're probably empty strings that are '_'s originally: readFile = ['Some name____2.0 2.1 1.3','Some other name_____2.2 3.4 1.1']

Here's how I would do it:

def openFile():
    readFile = ['Some name____2.0 2.1 1.3\n','Some other name_____2.2 3.4 1.1\n']

    data=[]

    for line in readFile:
        line = (line.rstrip()).split("_") #EDIT: Strip the newline character in this line
        data.append(line [-1].split(' '))

    print(data)

openFile()
abacles
  • 849
  • 1
  • 7
  • 15
  • So, what would you suggest? – Siyah Nov 16 '16 at 20:30
  • @EL_DON pointed out using `[-1]` indexing would get you the last element of the list. Since the numbers are at the end, you use `line [-1]` to get them. – abacles Nov 16 '16 at 20:32
  • Yes, but that one doesn't get the rid of the '\n' at the end of them. Plus, it keeps them formatted like this ['2.0 2.1 1.3',\n'] while I want 2.0, 2.1, 1.3 – Siyah Nov 16 '16 at 20:36
  • Oh, then strip the new line at the end first. I'll edit my answer. – abacles Nov 16 '16 at 20:38
  • 1
    No problem! Sorry I didn't realize there was a newline at the end at first! – abacles Nov 16 '16 at 20:42
2
blah=['name____2.0 2.0 1.3\n', 'aaahha____1.0 9.0 1.0\n', 'fasdkflj_________2 3 9.2']
blah2=[b.split('_')[-1].strip() for b in blah]

Output:

['2.0 2.0 1.3', '1.0 9.0 1.0', '2 3 9.2']

You can then split each item in this output with .split(' ') if you want actual numbers instead of strings containing numbers.

EL_DON
  • 1,416
  • 1
  • 19
  • 34
1
l = ['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n']

names = []
numbers = []
for line in l:
    line = line.strip().replace("_", " ").split()
    names.append(line[0] + " " + line[1])
    for s in line:
        try:
            numbers.append(float(s))
        except ValueError:
            pass
print(names)
print(numbers)
# ['Some Name', 'Some Name']
# [2.0, 2.0, 1.3, 1.0, 9.0, 1.0]

But FTW:

import re
l = ['Some Name__________2.0 2.0 1.3\n', 'Some Name__________1.0 9.0 1.0\n']
print([re.findall("[-+]?\d+[\.]?\d*[eE]?[-+]?\d*", s) for s in l])
John Smith
  • 1,077
  • 6
  • 21