Python Forum
how to change the range of read CSV file every time python file runs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to change the range of read CSV file every time python file runs
#7
thanks for the new code,
I run the new code:

csv_filename = "my.csv"
csv_read_counter_file = "csv_last_read.csv"
try:
    with open(csv_read_counter_file, "r") as f:
        lines_read = int(f.readline().rstrip())
except:
    # On any error, just reset back to start of file.
    lines_read = 0
 
with open(csv_filename, "r") as f:
    for index, line in enumerate(f):
        if index <= lines_read:
            continue
        print(line.rstrip())
 
with open(csv_read_counter_file, "w") as f:
    f.write(str(index))
now it prints all the data at once, I have now 20 rows of numbers in CSV file instead of 10 to test.
if I write a number in "csv_last_read.csv" and save the file, let's say 3, when I run the file, it prints all rows after row 3.
Reply


Messages In This Thread
RE: how to change the range of read CSV file every time python file runs - by greenpine - Dec-08-2020, 10:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 251 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 341 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 382 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Recommended way to read/create PDF file? Winfried 3 2,786 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,316 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 2,863 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,262 Sep-16-2023, 09:46 PM
Last Post: snippsat
  How to do 100 runs simulation based on the current codes? dududada 6 902 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020