1

I am taking an input text and a pattern from the user. Text is in the tabular format. Depending on the users choice, I want the code to give me all the columns of the row the user has selected using pattern. Eg. Text:

Name            Age            Sex          Address
ABC             22             Male         Bangalore
XYZ             34             Female       Hyderabad

Now the user gives input as 'XYZ' and the text given above. I want a code that would fetch me all the results i.e. 34,Female,Hyderabad in a list or in different variables. I tried using group but I am not able to understand how would I use group in this. Since 'XYZ' is given by the user, assumne it to be stored in a variable 'pattern'

3
  • Shouldn't you be using a TSV parser? Commented Mar 3, 2015 at 6:23
  • 1
    I agree, don't use a regex for this. The csv module is ideal for this. If Name is a unique identifier, then you just need a csv.reader() object and apply {row[0]: row[1:] for row in reader} to it. Commented Mar 3, 2015 at 6:26
  • @Biffen am new to python and I did not know about it. Thank you very much for your help. Commented Mar 3, 2015 at 6:35

1 Answer 1

1

You Can use this :

data=[]

inp=input()
data.append(inp.split(' '))

"""Now your first entry is data[0]
To get name , use data[0][0]
To get age , use data[0][1]
To get sex , use data[0][2]
and so on....
For 2nd Entry use :
To get name , use data[1][0]
To get age , use data[1][1]
To get sex , use data[1][2] """

You can put the code in a loop to get n entries. Hope this helps

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

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.