5 методов перебора строк в Python

  1. Использование цикла For:

    with open('file.txt', 'r') as file:
       for line in file:
           # Process each line here
  2. Использование метода Readlines():

    with open('file.txt', 'r') as file:
       lines = file.readlines()
       for line in lines:
           # Process each line here
  3. Использование итераторов:

    with open('file.txt', 'r') as file:
       lines_iterator = iter(file)
       for line in lines_iterator:
           # Process each line here
  4. Использование библиотеки Numpy:

    import numpy as np
    lines = np.genfromtxt('file.txt', delimiter='\n', dtype=str)
    for line in lines:
       # Process each line here
  5. Использование библиотеки Pandas:

    import pandas as pd
    lines = pd.read_csv('file.txt', sep='\n', header=None)
    for line in lines[0]:
       # Process each line here