0

My File:

Offices 10
MedicalOffice 15
PostOffice 30
Mall 200

How do I make the python to read only the second column. Like get:

10
15
30
200

I have tried possibly ways to make it only read, 10, 15, 30, etc... Instead of the names. I tried this without the names, it works fine. But I need to include the names in the textfile. Any help? Thanks

def something(file1):
    with file1 as f:
        nums = [int(line) for line in f]
        print("Read from File: ", nums)


textFileName = input("Enter the filename: ")
file1 = open(textFileName)
something(file1)

Thank you.

4
  • That's the second column. The second row is MedicalOffice 15. Commented Oct 9, 2013 at 1:52
  • @abarnert good catch there. My typo. Thanks for heads up! Commented Oct 9, 2013 at 1:54
  • Yeah, I figured it was just a typo, but better to fix it than to confuse future readers… Commented Oct 9, 2013 at 1:57
  • Meanwhile, you probably don't want to do the open at the top level, and the with inside the function. That does basically work, as long as you only ever call something in this one case, but it's strange. Normally you want the open directly in the with statement. Either with open(textFileName) as file1: something(file1) and then something doesn't have a with, or just something(textFileName) and inside something it opens the file in a with. (This is very hard to explain in a comment; hopefully you get the idea?) Commented Oct 9, 2013 at 1:59

3 Answers 3

3

To read the second column, split the lines and take the second field:

[x.split()[1] for x in open(textFileName,"r")]

If you want numbers, just call int:

[int(x.split()[1]) for x in open(textFileName,"r")]
Sign up to request clarification or add additional context in comments.

1 Comment

Why did you add the .readlines() here? It wasn't in the original question, and all it does is make the program use more memory and more CPU (and more typing) for no benefit.
2

You can't only read the second column.

But you can read both columns, ignore the first column, and only use the second.

For example:

def something(file1):
    with file1 as f:
        lines = (line.partition(' ') for line in f)
        nums = [int(line[-1]) for line in lines)
        print("Read from File: ", nums)

I did this in two steps, just to make it easier to see the part that's new (the partition) vs. the part you already had (the int). You can cram it all together into a single listcomp if you prefer:

        nums = [int(line.partition(' ')[-1]) for line in f]

Anyway, partition splits each line at the first space, so you get, e.g., ('Offices', ' ', '10'). The [-1] takes the last part, the '10'. And then there's the int, which you already know about.

Comments

1

Similar to @Nirk's answer, but with improved file handling:

with open('/path/to/file.txt', 'r') as f:
    nums = [x.strip().split()[-1] for x in f]

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.