Delete a row from csv file - python

name = input("Enter a name : ")
fieldnames = ["first_name", "number"]
with open('names.csv') as csvfile, open('output.csv', 'w') as outputfile:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(outputfile, fieldnames=fieldnames)
for line in reader:
if name not in line:
fieldnames = ["name", "number"]
writer.writeheader()
writer.writerow({'first_name': row['first_name'],
'number': row['number']})
with open('names.csv', 'w') as csvfile, open('output.csv') as outputfile:
reader = csv.DictReader(outputfile, fieldnames=fieldnames)
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for row in reader:
fieldnames = ['first_name', 'number']
writer.writeheader()
writer.writerow({'first_name': row['first_name'],
'last_name': row['number']})
This is what I've coded so far. I want to write the rows which doesn't include name variable in it to the output file and write it back to the csvfile (names.csv)
James Smith,2025550131
Kevin Harris,2025550105
This is how my csvfile looks like.
first_name,last_name
James Smith,2025550131
first_name,last_name
James Smith,2025550131
This is the names.csv file after I run the code.

row isn't declared in your first write loop, so running this in an IDE could have memorized the output of a semi-successful attempt and you have a weird issue.
You also write the header each time. Just don't, it's done by default.
Also: you're comparing to the keys, not to the values. if name not in row: checks if the name is a key, which cannot happen, keys are "first_name", and "number".
You need to do if name != row['first_name']:
Last but not least, no need to read/write the file again to replace the input, just perform a shutil.move to overwrite with the output.
My fix proposal (note newline='', better when using python 3 to avoid blank lines on some not so up-to-date versions):
import shutil,csv
name = input("Enter a name : ")
fieldnames = ["first_name", "number"]
with open('names.csv') as csvfile, open('output.csv', 'w',newline='') as outputfile:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(outputfile, fieldnames=fieldnames)
for row in reader: # row, not line !!
if name != row['first_name']:
writer.writerow({'first_name': row['first_name'], 'number': row['number']})
# replace original file
shutil.move('output.csv','names.csv')

You need to read the input name.csv file.
Check in name matches the input name, then ignore that row.
If it does not match then write the content to output file.
In the end just copy the output file over the original name.csv file.
import csv, shutil
name = 'vikash' # This you can take as input from user
fieldnames = ["first_name", "number"]
with open('names.csv', 'r') as csvfile, open('output.csv', 'w') as outputfile:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(outputfile, fieldnames=fieldnames)
for row in reader:
if not name == row['first_name']:
writer.writerow({'first_name': row['first_name'], 'number': row['number']})
shutil.move('output.csv','names.csv')
names.csv
first_name,number
vikash,1
viki,2
pawan,3
output.csv
first_name,number
viki,2
pawan,3
The reason why your code didn't work:
name = input("Enter a name : ")
fieldnames = ["first_name", "number"]
with open('names.csv') as csvfile, open('output.csv', 'w') as outputfile:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(outputfile, fieldnames=fieldnames)
for line in reader:
# you are reading line from reader but you are using row later.
if name not in line:
fieldnames = ["name", "number"]
# above line is not needed as its already initialised earlier.
writer.writeheader()
# this is not required as header will be written automatically.
# Plus you also don't want to write header ever time you write a row to the output file.
writer.writerow({'first_name': row['first_name'], 'number': row['number']})
# use line variable here or change line above to row.

Related

CSV adding values to an existing column and calling from a dictionary (no pandas)

I need to add some additional values to an existing column for my CSV file.
So this is what I have so far:
Sample input CSV file:
Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1
A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2
A3,B3,C3,D3,E3,F3,G3,H3,I3,J3,K3
A4,B4,C4,D4,E4,F4,G4,H4,I4,J4,K4
A5,B5,C5,D5,E1,F5,G5,H5,I5,J5,K5
A6,B6,C6,D6,E6,F6,G6,H6,I6,J6,K6
A7,B7,C7,D7,E7,F7,G7,H7,I7,J7,K7
A8,B8,C8,D8,E8,F8,G8,H8,I8,J8,K8
A9,B9,C9,D9,E9,F9,G9,H9,I9,J9,K9
This is what I have so far, I am thinking of converting row_out into a list and then inputing the values under India_New.
import csv
fieldnames_dict = {
'Beta': 'Beta_New',
'Echo': 'Echo_New',
'Foxtrot': 'Foxtrot_New_ALL',
'Hotel': 'Hotel_New',
'India': 'India_New',
'Charlie': 'Charlie_New'
}
with open("book1.csv", "r", encoding="utf-8", errors='ignore') as csv_in:
with open("xtest_file.csv", "w", encoding="utf-8", errors='ignore') as csv_out:
reader = csv.DictReader(csv_in, delimiter=',', quotechar='"')
writer = csv.DictWriter(csv_out, delimiter=',', quotechar='"',
fieldnames=list(fieldnames_dict.values()))
writer.writeheader()
additional_values = [';I_1;I_2']
new_row_out = []
for row_in in reader:
row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
row_out.items()
for row in row_out.items():
new_row_out.append(row_out.items())
writer.writerow(new_row_out)
What the output CSV should look like:
Beta_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
B1,E1,F1,H1,I1;I_1;I_2,C1
B2,E2,F2,H2,I2;I_1;I_2,C2
B3,E3,F3,H3,I3;I_1;I_2,C3
B4,E4,F4,H4,I4;I_1;I_2,C4
B5,E5,F5,H5,I5;I_1;I_2,C5
B6,E6,F6,H6,I6;I_1;I_2,C6
B7,E7,F7,H7,I7;I_1;I_2,C7
B8,E8,F8,H8,I8;I_1;I_2,C8
B9,E9,F9,H9,I9;I_1;I_2,C9
Since writer is a DictWriter, the argument to writer.writerow() must be a dictionary, not a list like new_row_out.
Just append the string to the India_New item in the row_out dictionary before writing it.
with open("book1.csv", "r", encoding="utf-8", errors='ignore') as csv_in, open("xtest_file.csv", "w", encoding="utf-8", errors='ignore') as csv_out:
reader = csv.DictReader(csv_in, delimiter=',', quotechar='"')
writer = csv.DictWriter(csv_out, delimiter=',', quotechar='"',
fieldnames=list(fieldnames_dict.values()))
writer.writeheader()
additional_india_new = ';I_1;I_2'
for row_in in reader:
row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
row_out['India_New'] += additional_india_new
writer.writerow(row_out)

Append row to CSV adding header as a row

I have the following code which I am using to create and add a new row to a csv file.
def calcPrice(data):
fieldnames = ["ReferenceID","clientName","Date","From","To","Rate","Price"]
with open('rec2.csv', 'a') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(data)
return
However, it as the header as a new row as well. How can I prevent this?
The writeheader method writes a header row, so it should only be used on an empty file.
You should first test whether the file was or not empty and only write the header line on an empty file:
import os
...
with open('rec2.csv', 'a') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if 0 == os.lseek(csvfile, 0, os.SEEK_END):
writer.writeheader()
writer.writerow(data)

Updating a specific row in csv file

I have a csv file like this:
Name,PhoneNumber,Adress
I want to get input from the user and change the name. I can delete the whole row.
name = input("Enter a name : ")
fieldnames = ["name", "number", 'address']
with open('book.csv', 'r') as csvfile, open('outputfile.csv', 'w') as output:
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
writer = csv.DictWriter(output, fieldnames=fieldnames)
for row in reader:
if not name == row['name']:
writer.writerow({'name': row['name'], 'number': row['number'], 'address': row['address']})
shutil.move('outputfile.csv','book.csv')
This is my deleting code.
if name matches, just interactively prompt for a new name, updating row:
for row in reader:
if name == row['name']:
row['name'] = input("enter new name for {}".format(name))
# write the row either way
writer.writerow({'name': row['name'], 'number': row['number'], 'address': row['address']})
Aside: for maximum compatibility (specially on windows), python 2 needs open('outputfile.csv', 'wb') (or you get blank lines in output file) and python 3 needs open('outputfile.csv', 'w', newline='').

Python csv register_dialect delimiter is not working

I have the written the code below to read in a large csv file with many variables and then just print 1 variable for every row in the outfile. It is working except that the delimiter is not being picked up.
import csv
fieldnames = ['tag']
outfile = open('ActiveTags.txt', 'w')
csv.register_dialect('me', delimiter=',', quotechar="'", quoting=csv.QUOTE_ALL, lineterminator='')
writer = csv.DictWriter(outfile, fieldnames=fieldnames, dialect='me')
with open('ActiveList_16.csv', 'r', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
Tag = row['Tag']
writer.writerow({'tag': Tag})
outfile.close()
What am I missing here? I do not understand why the delimiter is not working on the outfile.

python write to new column in csv file

I want to add a new column to an existing file. But it gets a little complicated with the additional loops i add.
input file:
testfile.csv
col1,col2,col3
1,2,3
3,4,5
4,6,7
output i want:
USA_testfile.csv
col1,col2,col3,country
1,2,3,USA
3,4,5,USA
4,6,7,USA
UK_testfile.csv
col1,col2,col3,country
1,2,3,UK
3,4,5,UK
4,6,7,UK
This is what i have tried:
import csv
import sys
country_list= ['USA', 'UK']
def add_col(csv_file):
for country in country_list:
with open(csv_file, 'rb') as fin:
with open(country+"_timeline_outfile_"+csv_file, 'wb') as fout:
writer = csv.writer(fout, lineterminator='\n')
reader = csv.reader(fin)
all_rows =[]
row = next(reader)
row.append('country')
all_rows.append(row)
print all_rows
for row in reader:
row.append(country)
all_rows.append(row)
writer.writerows(all_rows)
add_col(sys.argv[1])
And this is the error i got:
File "write_to_csv.py", line 33, in add_col
writer.writerows(all_rows)
ValueError: I/O operation on closed file
I was trying to follow this post here
import csv
countries = ['USA', 'UK']
data = list(csv.reader(open('testfile.csv', 'rb')))
for country in countries:
with open('{0}_testfile.csv'.format(country), 'wb') as f:
writer = csv.writer(f)
for i, row in enumerate(data):
if i == 0:
row = row + ['country']
else:
row = row + [country]
writer.writerow(row)
I couldn't reproduce your error, but i cleaned your code a bit.
There is no reason to reopen the input file for every language.
def add_col(csv_file):
with open(csv_file, 'rb') as fin:
reader = csv.reader(fin)
for country in country_list:
fin.seek(0) # jump to begin of file again
with open(country+"_timeline_outfile_"+csv_file, 'wb') as fout:
writer = csv.writer(fout, lineterminator='\n')
header = next(reader)
header.append('country')
writer.writerow(header)
for row in reader:
row.append(country)
writer.writerow(row)

Categories