0

I am currently working on a project and I need to test if, on the last row (line) of the input, I have this byte: '\x1a'. If the last row has this marker I want to delete the entire row.

I have this code so far, but i don't know how to make it test for that byte on the last row and delete it.

Thank you!

readFile1 = open("sdn.csv")
lines1 = readFile1.readlines()
readFile1.close()
w1 = open("sdn.csv", 'w')
w1.writelines([item for item in lines1[:-1]])
w1.close()

readFile2 = open("add.csv")
lines2 = readFile2.readlines()
readFile2.close()
w2 = open("add.csv",'w')
w2.writelines([item for item in lines2[:-1]])
w2.close()

readFile3 = open("alt.csv")
lines3 = readFile3.readlines()
readFile3.close()
w = open("alt.csv",'w')
w.writelines([item for item in lines3[:-1]])
w.close()
trent
  • 25,033
  • 7
  • 51
  • 90
Cohen
  • 944
  • 3
  • 13
  • 40
  • I have edited your question to make it clearer, but I may have made a mistake. Please let me know if I misunderstood the question. (Or just edit and fix it yourself.) – trent Sep 11 '17 at 14:38
  • If you're on Windows, opening the file in text mode (the default) will automatically remove the `\x1a` character from the end of the file - your code won't even see it. – Mark Ransom Sep 11 '17 at 15:38

1 Answers1

2

In any of your code blocks, you have read your file's contents into a variable with a line like:

lines1 = readFile1.readlines()

If you want to see if the \x1a byte exists anywhere in the last line of the text, then you can do this:

if '\x1a' in lines1[-1]:
    # whatever you need to do

If you want to find the byte and then actually delete the row from the list altogether:

if '\x1a' in lines1[-1]:
    # \x1a byte was found, remove the last item from list
    del lines1[-1]

And if I may offer a suggestion, all your code blocks repeat. You could create a function which captures all the functionality and then pass file names to it.

def process_csv(file_name):
    # Open the file for both reading and writing
    # This will also automatically close the file handle after
    # you're done with it
    with open(file_name, 'r+') as csv_file:
        data = csv_file.readlines()
        if '\x1a' in data[-1]:
            # erase file and then write data without last row to it
            csv_file.seek(0)
            csv_file.truncate()
            csv_file.writelines(data[:-1])
        else:
            # Just making this explicit
            # Don't do anything to the file if the \x1a byte wasn't found
            pass

for f in ('sdn.csv', 'add.csv', 'alt.csv'):
    process_csv(f)
wkl
  • 77,184
  • 16
  • 165
  • 176
  • `if '\x1a' in lines[-1]` is probably a little nicer than using `.find()` – trent Sep 11 '17 at 15:20
  • thank you birryree! I've learned something today! thank you for taking time to answer my question! `[-1]:` this means the last line? i had no clue how i can see the last line. your function is soo pythonic and i can see your skills in python here! cheers! – Cohen Sep 11 '17 at 16:02
  • @JohnCohen - Python `list`s and tuples can be sliced multiple ways (see [other SO answer](https://stackoverflow.com/questions/509211/explain-slice-notation)). But yes `some_list[-1]` means the last item of a list. It's a lot nicer than doing something like `some_list[len(some_list) - 1]`. – wkl Sep 11 '17 at 16:06