Re:Don’t Slurp: How to Read Files in Python
by paulgb on Friday September 27, 2013 @07:47PM
(#6460251)
I agree that it's good to have an easy way to read a whole file in, but when I think about it I can't think of any case where I've had to write code to read a file that I didn't want to process line-by-line, in which case the non-slurping method is actually less code.
[ Parent
| on HN ]
Re:Don’t Slurp: How to Read Files in Python
by j_baker on Saturday September 28, 2013 @12:53PM
(#6462319)
It sounds like you've only read in line-by-line files then. I mean, it doesn't make sense to read in a JSON or XML document line-by-line. Nor does it make sense to read in most binary files line-by-line.
Some formats, such as csv do make sense to read in line-by-line though.
Re:Don’t Slurp: How to Read Files in Python
by paulgb on Sunday September 29, 2013 @09:14PM
(#6467277)
I've read in lots of formats, including many XML and binary files. 99% of the time there is a library that already handles the low-level interaction with those files. The only times I've had to write original Python code that reads files directly have typically been things that could be processed line by line (eg various record-based data formats). I'm trying to think of a counter example but I'm coming up blank.
depends on how much you care about performance/memory usage.
for one, variance in the line lengths (by byte count) will probably force the usage of inefficient buffer sizes
also, the parent comment gave one universal example: binary files