0

I have a text file structure like:

X   Y   Home
X   Y   School

X and Y are times and the last column is a location. How can I use python to assess the X and Y element where the 3rd Column is Home?

Note: I have already opened the text file. If you feel I am asking to much, I would appreciate a link to a place that tells how to do something like this. Also, X and Y are not numbers they are strings: ex. 4:30 AM

3
  • 1
    How do you distinguish between spaces in X and Y (like "4:30 AM") and field separators? Commented Apr 22, 2014 at 1:54
  • Well, I am going to treat them like 2 columns and combine them after I read them. Commented Apr 22, 2014 at 2:01
  • It really depends on what your trying to do. Commented Apr 22, 2014 at 2:01

1 Answer 1

1

Assuming the contents of the file have been dumped to a variable, one way is to parse every line like so:

for line in file_content:
     columns = line.split()
     x_1 = columns[0]
     x_2 = columns[1]
     home = columns[2]
     ...
     ...

Basically the split distinguishes X, Y, Home and School and places them into a list. So ultimately, columns is a list containing [X, Y, Home] regardless of the type of data.

Sign up to request clarification or add additional context in comments.

2 Comments

So the "for line in file_content:" will loop through all the lines in file content (variable containing all of the file contents)?
Yes, exactly (assuming formatting does not cause any issues)

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.