I'm brand new to python, and trying to use it to sort through a .csv file. The file looks like below:
Date | Username | Full Name | Office | User Dept | Card Num | Charged Acct | Shared Acct | |
---|---|---|---|---|---|---|---|---|
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | USER | |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | USER | |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | USER | |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | USER | |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | USER | |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | SHARED | ACCT1 |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | SHARED | ACCT2 |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | SHARED | ACCT3 |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | SHARED | ACCT4 |
4/20/2020 0:00 | jdoe | Jane Doe | 1 | HR | jdoe@google.com | 12345 | SHARED | ACCT5 |
My goal is to remove all rows where the Charged Acct column contains "USER". I'm first setting up a list like below, to export later into a .csv with all the user rows removed:
lines = list()
with open('C:/temp/pythonproject.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
lines.append(row)
When I try to remove the rows with USER in the 8th column I'm using the below snippet and getting the below output where it only removes 3 of the instances and I can't for the life of me figure out why. Can anyone see anything wrong?
for row in lines:
if row[7] == 'USER':
lines.remove(row)
for row in lines:
print(row[7])
Output:
Charged Acct
USER
USER
SHARED
SHARED
SHARED
SHARED
SHARED