Python Forum
Deleting a line in a csv file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Deleting a line in a csv file (/thread-24642.html)



Deleting a line in a csv file - julio2000 - Feb-24-2020

I just can't figure out how to do this :(

I've got a csv file with this content:
Main,Daan,Daanssen,[email protected],Tennon straat,39,4124KE,Buren,0634582938
Second,Jens,Daanssen,[email protected],Tennon straat,30,4124KE,Endracht,0634582938
Third,harry,Daanssen,[email protected],Tennon straat,30,4124KE,Endoort,0634582938

I want to create a piece of code, that is able to delete a whole line by choice.
The user gives the first word of the line (for example: Main), and the code should delete the line that starts with the word Main.

Does someone know how to do this? Thanks in advance!


RE: Deleting a line in a csv file - micseydel - Feb-24-2020

I would loop through the contents of the file and write each one to a new file, unless it qualifies for skipping. It should be pretty straightforward, if you give it a try we'd be happy to help with any details that you have trouble with. (Note, you can't modify files in-place in this way, though you can create the illusion of doing so by renaming the new file to the old file's name.)


RE: Deleting a line in a csv file - julio2000 - Feb-24-2020

import pandas as pd

file = pd.read_csv('Profiles.txt', delimiter=',')
counter = 0
while True:
    try:
        profile = file.iloc[counter, 0]
    except Exception:
        print('%s ERROR: No profiles loaded%s' % (fg(196), attr(0)))
        time.sleep(2)
        clear()
        profiles()
    else:
        if profile == want_delete:
            line_delete = teller + 1
            break
        else:
            counter += 1

# HERE SHOULD COME THE CODE THAT DELETES THE LINE: line_delete. 
Have you got advice on how to do this?

(Feb-24-2020, 11:12 PM)julio2000 Wrote:
import pandas as pd

# THE OTHER PART OF THE CODE IS NOT IMPORTANT

file = pd.read_csv('Profiles.txt', delimiter=',')
counter = 0
while True:
    try:
        profile = file.iloc[counter, 0]
    except Exception:
        print('%s ERROR: No profiles loaded%s' % (fg(196), attr(0)))
        time.sleep(2)
        clear()
        profiles()
    else:
        if profile == want_delete:
            line_delete = teller + 1
            break
        else:
            counter += 1

# HERE SHOULD COME THE CODE THAT DELETES THE LINE: line_delete. 
Have you got advice on how to do this?

(Feb-24-2020, 11:12 PM)julio2000 Wrote:
import pandas as pd

# THE OTHER PART OF THE CODE IS NOT IMPORTANT

file = pd.read_csv('Profiles.txt', delimiter=',')
counter = 0
while True:
    try:
        profile = file.iloc[counter, 0]
    except Exception:
        print('%s ERROR: No profiles loaded%s' % (fg(196), attr(0)))
        time.sleep(2)
        clear()
        profiles()
    else:
        if profile == want_delete:
            line_delete = teller + 1
            break
        else:
            counter += 1

# HERE SHOULD COME THE CODE THAT DELETES THE LINE: line_delete. 
Have you got advice on how to do this?



RE: Deleting a line in a csv file - snippsat - Feb-25-2020

You most tell in your post @julio2000 that you are gone use Pandas.
Then the answer bye micseydel will not be the way as Pandas has it own ways of doing most stuff,
as example not looping trough file as would in do regular Python code.
>>> import pandas as pd

>>> file = pd.read_csv('csv_f.csv', delimiter=',', header=None)
>>> file
        0      1         2             3              4   5       6         7          8
0    Main   Daan  Daanssen  [email protected]  Tennon straat  39  4124KE     Buren  634582938
1  Second   Jens  Daanssen  [email protected]  Tennon straat  30  4124KE  Endracht  634582938
2   Third  harry  Daanssen  [email protected]  Tennon straat  30  4124KE   Endoort  634582938

>>> del_row_with = 'Main'
>>> df = file[~file[0].str.startswith(del_row_with)]
>>> df
        0      1         2             3              4   5       6         7          8
1  Second   Jens  Daanssen  [email protected]  Tennon straat  30  4124KE  Endracht  634582938
2   Third  harry  Daanssen  [email protected]  Tennon straat  30  4124KE   Endoort  634582938



RE: Deleting a line in a csv file - julio2000 - Feb-25-2020

Well i would prefer to not use pandas, cause with pandas you'll need to use row titles and stuff. But panda seemed the best option to do this I believe.

Btw thanks! It does work in the python console, but i don't see any changes in the csv file, wich i do want to happen. do you know why de csv file doesn't change?


RE: Deleting a line in a csv file - snippsat - Feb-25-2020

(Feb-25-2020, 11:24 AM)julio2000 Wrote: but i don't see any changes in the csv file, wich i do want to happen. do you know why de csv file doesn't change?
Have to save the DataFrame,take away index and header then get a clean .csv if that's what you want.
import pandas as pd

file = pd.read_csv('csv_f.csv', delimiter=',', header=None)
del_row_with = 'Main'
df = file[~file[0].str.startswith(del_row_with)]
print(df)
df.to_csv(r'out.csv', index=False, header=False)
Output:
Second,Jens,Daanssen,[email protected],Tennon straat,30,4124KE,Endracht,634582938 Third,harry,Daanssen,[email protected],Tennon straat,30,4124KE,Endoort,634582938



RE: Deleting a line in a csv file - julio2000 - Feb-25-2020

yess perfect! It works! thanks a lot!!