0

Hello am a beginner on python and i want to know if ther's away to read a line and transform it to a namedtuple i will explain i have this 2 lines

MARKS      CLASS      NAME       ID        
92         2          Calum      1 

For the first line :

line = raw_input()
person = namedtuple('person',line)

and i made my namedtuple

now while i 'am reading the second line i don't know how to insert those informations in a namedtuple i tried to split it i tried all this methods but i coudn't make it

>>> line = raw_input().split(' ')
92         2          Calum      1 
>>> line
['92', '', '', '', '', '', '', '', '', '2', '', '', '', '', '', '', 
'', '', '', 'Calum', '', '', '', '', '', '1', '']
>>> line = raw_input().split('/t')
92         2          Calum      1 
>>> line
['92         2          Calum      1 ']
>>> line = raw_input().split('        ')
92         2          Calum      1 
>>> line
['92', ' 2', '  Calum      1 ']
>>> 

Whatever what i am trying i can't have 4 arguments So please if you have any idea how can i split this string into columns help me thank you cordially

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

2 Answers2

3

Just using split() will split at any whitespace characters (tabs, or multiple spaces). So

line = raw_input()
args = line.split()

Then, because namedtuple accepts multiple arguments, not a list, you just have to unwrap the argument list, using the * operator.

person = Person(*args)
blue_note
  • 27,712
  • 9
  • 72
  • 90
0

So the answer according to me would be to use '\t' instead of '/t'

a=raw_input().split('\t')
Ravinder Baid
  • 395
  • 1
  • 15