1

I'm a beginner py.

I need to extract the contents of column 12 from a huge flat text file every time a certain word is found within the text file (similar to awk '{print $12}' in Bash) in a Python script.

So far I have (example, not real code):

word = a_word
for a_word in data:
   column12 = data.split()
   print(column12[11])

I assumed that I should start counting from 0 as opposed to 1, though I very well may be incorrect.

Also, is a for loop correct for this type of code?

Thanks!

1
  • 2
    What format is your data in? Could you post a row or two of sample data? Commented Aug 20, 2013 at 0:10

2 Answers 2

8

Loop over the open file object:

with open('somefile') as infile:
    for line in infile:
        print(line.split()[11])

So, yes, use a for loop and use 0-based indexing.

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

2 Comments

if we need to split 2 columns ? I tried print(line.split()[1][2]) It says print(line.split()[0][1]) IndexError: string index out of range
@SitzBlogz: str.split() returns a list object. If you need two columns from that, then assign the output of line.split() to a variable, and pick out the two elements you wanted from that list.
1

If the columns are text files with delimiter (i.e. space or comma) separated values, you might want to look into python's csv module: http://docs.python.org/2/library/csv.html

1 Comment

Fields in csv files can be separated by delimiters other than comma, so that not a requirement. So, I agree using it should be considered.

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.