Python Forum
Trying to delete rows above a specific datetime value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to delete rows above a specific datetime value
#3
I sometimes do a similar thing in MySQL, if I forget to set the correct date first.

As far as I know, you can compare dates directly。
Assuming your data is in ascending order, you can find the position where you want to cut off an take a slice.

def myApp():
    import csv, glob

    path2file = '/home/pedro/myPython/csv/'
    files = glob.glob(path2file + '*.csv')
    for f in files:
        print('The files are', f)

    myfile = input('Copy and paste a file here ... ')
    with open(myfile) as infile:
        my_csv_read = csv.reader(infile)
        data = []
        # csv reader is burned as soon as you read it, so make a list, lasts longer
        for line in my_csv_read:
            data.append(line)
            print(line)

    # working principle    
    x = '2022-04-25 7:07 PM'
    y = '2022-04-25 7:08 AM'

    if x > y:
        print('x is older than y')
    if x < y:
        print('x is younger than y')
        
    nums2delete = []
    # start at 1 if you have a headers row
   for i in range(1, len(data)):
        if data[i][0] >= '2022-04-25 7:07 AM':
            break
            
    wanted = data[0:i]
Reply


Messages In This Thread
RE: Trying to delete rows above a specific datetime value - by Pedroski55 - May-06-2022, 07:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  delete specific row of entries jacksfrustration 3 554 Feb-13-2024, 11:13 PM
Last Post: deanhystad
  Reading Specific Rows In a CSV File finndude 3 1,101 Dec-13-2022, 03:19 PM
Last Post: finndude
  Pymysql delete specific rows in tableview stsxbel 2 1,182 Aug-18-2022, 09:50 AM
Last Post: ibreeden
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,705 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,408 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  openpyxl incorrect delete rows VladislavM 6 4,386 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,207 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,578 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to sort rows based on specific order Mekala 3 2,330 Jul-31-2020, 01:01 AM
Last Post: bowlofred
  How to extract specific rows and columns from a text file with Python Farhan 0 3,496 Mar-25-2020, 09:18 PM
Last Post: Farhan

Forum Jump:

User Panel Messages

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