Advertisement
furas

Python - split file - (Stackoverflow)

May 30th, 2024 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. # [python - Finding a specific keyword while dividing the large csv into smaller segments, and moving to next row until keyword is found - Stack Overflow](https://stackoverflow.com/questions/78555996/finding-a-specific-keyword-while-dividing-the-large-csv-into-smaller-segments-a)
  2.  
  3. other_list = []  # for lines
  4. index = 0        # for filenames
  5.  
  6. with open("combined.con") as infile:
  7.     for line in infile:
  8.         other_list.append(line)
  9.         if len(other_list) >= 5 and line.startswith('END'):
  10.             with open(f"Contingency_{index}.con", 'w') as outfile:
  11.                 for item in other_list:
  12.                     outfile.write(item)
  13.             index += 1
  14.             # clear list for next file
  15.             other_list = []
  16.                    
  17. # make sure there is no data (without `END` in last line)
  18. if len(other_list) > 0:
  19.     with open(f"Contingency_{index}.con", 'w') as outfile:
  20.         for item in other_list:
  21.             outfile.write(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement