I'm having another crack at python - and I am enjoying the clean simple syntax - but one thing I couldn't work out was how to read a line from a file until a '\t' character.

How could I do this?

It is a phone-book program and the data is stored as follows:

Name + '\t' + Number + '\n'

Recommended Answers

All 4 Replies

You can use

name, number = line.strip().split()

Gribouillis could you explain that a little please

Gribouillis could you explain that a little please

line.strip() returns a string with white space removed at both ends (white space is a space or tabulation or newline character). line.split() splits the line into pieces separated by white space and returns a list of the pieces. For example

>>> line = "John\t000000000000000\n"
>>> print( line.strip().split() )
['John', '000000000000000']

Then name, number = ['John', '000000000000000'] defines 2 string variables, name has value 'John' and number has value '000000000000000'.

thankyou <3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.