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']})
这是我迄今为止所编码的内容。我想将不包含name变量的行写入输出文件并将其写回csvfile(names.csv)
James Smith,2025550131
Kevin Harris,2025550105
这就是我的csvfile的样子。
first_name,last_name
James Smith,2025550131
first_name,last_name
James Smith,2025550131
这是运行代码后的names.csv文件。
答案 0 :(得分:3)
row
未在您的第一个写循环中声明,因此在IDE中运行此操作可能会记住半成功尝试的输出,并且您有一个奇怪的问题。
您每次都会写标题。不管怎样,默认情况下已经完成了。
另外:您需要与键进行比较,而不是与值进行比较。 if name not in row:
检查名称是否为密钥,密钥不是"first_name",
和"number"
。
您需要if name != row['first_name']:
最后但并非最不重要的是,无需再次读取/写入文件来替换输入,只需执行shutil.move
覆盖输出。
我的修正提案(注意newline=''
,在使用python 3避免某些不是最新版本的空行时更好):
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')
答案 1 :(得分:0)
您需要阅读输入name.csv文件。
签入名称与输入名称匹配,然后忽略该行。
如果不匹配,则将内容写入输出文件。
最后,只需将输出文件复制到原始的name.csv文件中。
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
您的代码无效的原因:
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.