0

I have made a csv file in python and then have imported it to python. Now I want to write a for loop and if statement to remove one specific row from the file. The same way as I print the specific row by using specific element from the row.

Here is how a get access to a row:

    data=[]
with open("platsbiljet.csv") as csvfile:
    reader=csv.reader(csvfile)
    for row in reader:
        data.append(row)

print(data)

lookup = input("Please enter your seat number:")
colx= [s[4] for s in data]
print(colx)

if lookup in colx:
    for k in range(0, len(colx)):
        if colx[k]==lookup:
            print(data[k])

else:
    print("No seat with that number!")
Mahsa
  • 1
  • 2

1 Answers1

0

Since all your rows are read and stored in a list.

You can use the following to delete an element from list (More info here):

del data[k]

Then, you can save the CSV:

with open('your_file.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(data)
Rithin Chalumuri
  • 1,739
  • 7
  • 19
  • Thanks for your help. but when I run this code I get an error "IndexError: list assignment index out of range" – Mahsa Nov 22 '19 at 16:23