[Tutor] How to parse large files

Peter Otten __peter__ at web.de
Sun Nov 1 18:17:19 EST 2015


jarod_v6--- via Tutor wrote:

> Thanks!!
> I use python2.7 Can Also use in that version?

Yes.

> I don't understand why use partition and not  split(). what is the reason
> for that?

If there is exactly one "\t" in the line

key, value = line.split("\t")

and

key, _tab, value = line.partition("\t")

are equivalent, so it's fine to use split(). I used partition() because the 
method name indicates that I want two parts, but with the extra assert

parts = line.split("\t")
if len(parts) != 2:
    raise ValueError
key, value = parts

would probably have been the better choice because it works the same with 
enabled and disabled asserts.




More information about the Tutor mailing list