Search code examples
pythonpython-3.xsplitreadlinepython-itertools

Does a fast Python built-in method for reading lines and then splitting them exist?


This method works just fine in Python:

with open(file) as f:
    for line in f:
        for field in line.rstrip().split('\t'):
            continue

However, it also means I read each line twice. First I loop over each character of the file and search for newline characters and second I loop over each character of the line and search for tab spaces. Is there a built-in method for splitting lines, while avoiding looping over the same set of characters twice? Apologies if this is a stupid question.


Solution

  • If you're worried about this level of efficiency then you probably shouldn't be programming in Python. Most of what is happening in that loop happens in C (if you're using the CPython implementation). You're not going to find a more efficient way to process your data using a pure python approach or without creating a very complicated looping structure.