Python delete row in file after reading it

2024/11/16 18:40:45

I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i dont waste to much of CPU.

    read = open("data.csv", 'r')for row in read:#code......if send == True:-->delete sent line from file, and continue to loop
Answer

You shouldn't concern yourself about cpu usage when doing disk IO -- disk IO is very slow compared to almost any in-memory/cpu operation.

There are two strategies to deleting from the middle of a file:

  1. writing all lines to keep to a secondary file, then renaming the secondary file to the original file name.

  2. copy the rest (tail) of the file to the beginning of the line you want to delete and then truncating x bytes off the end of the file (where x is equal to the length of the line you want to remove.

Number 1 is usually preferred since it is easier and doesn't require any locking.

Mayank Porwal has given you most of strategy #1. Here is how you would implement strategy #2:

# open the file for both reading and writing in binary mode ('rb+')
with open('rmline.txt', 'rb+') as fp:   while 1:pos = fp.tell()       # remember the starting position of the next line to readline = fp.readline()if not line:break  # we reached the end of the fileif should_line_be_skipped(line):  # only you know what to skip :-)rest = fp.read()  # read the rest of the filefp.seek(pos)      # go to the start position of the line to removefp.write(rest)    # write the rest of the file over the line to be removedfp.truncate()     # truncates at current position (end of file - len(line))fp.seek(pos)      # return to where the next line is after deletion so we can continue the while loop
https://en.xdnf.cn/q/120296.html

Related Q&A

Trying to keep the same type after saving a dataframe in a csv file

When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?

Merge blocks of images to produce new image

Hi is there a way of merging specific blocks from multiple images of same size(say 100x100) and putting them together in a new image. To be more specific, consider I have a set of images which have bee…

Removing Characters from python Output

I did alot of work to remove the characters from the spark python output like u u u" [()/" which are creating problem for me to do the further work. So please put a focus on the same .I have …

How to make a tkinter entry default value permanent

I am writing a program in python that will take in specific formats, a Phone number and dollar/cent values. How can I make tkinter have default value which is permanent, not deletable. For example (XXX…

distribute value in buckets

Consider below DF, I have an input number=4 to be inserted evenly in different hour buckets.p_hourly mins 0 2020-09-10 07:00:00 60.0 1 2020-09-10 08:00:00 60.0 2 2020-09-10 09:00:00 60…

for loop over list break and continue

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entr…

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…

How can I filter the domains served by a CDN from a list of domain names?

I have a list of domains and I need to filter the domains served by a CDN(Content Delivery Network). I am going to use python script to do that. At the first I was thinking I can identify them from the…

Convert int(round(time.time())) to C# [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to iterate over all elements of a 2D matrix using only one loop using python

I know you can iterate over a 2d matrix using two indexes like this: import numpy as npA = np.zeros((10,10))for i in range(0,10):for j in range(0,10):if (i==j):A[i,j] = 4Is there a way of doing this us…