好的,我一直在网站上搜索解决方案,我发现了一些,但这些情况对我不利,主要是因为他们在'wb'
中使用csv
模式文件,这是我的问题的论点是不同的,我使用DictReader
和Writer
。我基本上,我想循环我的csv
文件中的数据,如果数据匹配什么放在delete_entry
内,我想从line
文件中完全删除csv
。我曾经想过用line
文件中的数据覆盖不需要的csv
,但我不知道如何处理该解决方案。我认为这是一个合乎逻辑的解决方案,但最终发生的是每一行都被删除。
import csv
def delete_information_method():
#a tkinter entry
global delete_entry
#opening csv in read
with open('employees.csv' , 'r') as emp_read:
#creating our dictreader
csv_dictreader = csv.DictReader(emp_read)
#opening csv file again in write mode
with open ('employees.csv' , 'w') as emp_write:
#creating our writer
csv_writer1 = csv.writer(emp_write)
#our loop to check each line inside of our csv file is equal to what is inside the delete entry
for line in csv_dictreader:
if line['employee id'] == str(delete_entry):
del line
first name,last name,email,DOB,adress,position,employee salary,employee id
ronald,colyar,N/A,1-1-2029 ,11245 s. plaski,software dev,233333,233
答案 0 :(得分:0)
这里有几点:
您需要以rb
/ wb
模式打开文件(如果在Python 2上)或在处理CSV文件时使用Python 3上的newline=''
参数。至少如果您的数据中可能出现换行符。
当文件仍然可供阅读时,您无法打开该文件,特别是因为此时您还没有从中读取任何数据。
< / LI>你永远不会写任何文件。你唯一要做的就是在写模式下打开它(它将它作为第一件事将其删除)。
最好使用不同的输出文件:
#assuming Python 3
def delete_information_method():
#a tkinter entry
global delete_entry
#opening csv in read
with open('employees.csv' , 'r', newline='') as emp_read:
#creating our dictreader
csv_dictreader = csv.DictReader(emp_read)
fieldnames = csv_dictreader.fieldnames
#opening new csv file in write mode
with open ('employees_out.csv' , 'w', newline='') as emp_write:
#creating our writer
csv_writer1 = csv.DictWriter(emp_write, fieldnames=fieldnames)
csv_writer1.writeheader()
#our loop to check each line inside of our csv file is equal to what is inside the delete entry
for line in csv_dictreader:
if line['employee id'] != str(delete_entry.get()):
csv_writer1.writerow(line)
如果您不想这样做,请阅读整个文件,然后再写下来:
def delete_information_method():
#a tkinter entry
global delete_entry
#opening csv in read
with open('employees.csv' , 'r', newline='') as emp_read:
#creating our dictreader
csv_dictreader = csv.DictReader(emp_read)
fieldnames = csv_dictreader.fieldnames
contents = [line for line in csv_dictreader]
#opening csv file in write mode
with open ('employees.csv' , 'w', newline='') as emp_write:
#creating our writer
csv_writer1 = csv.DictWriter(emp_write, fieldnames=fieldnames)
csv_writer1.writeheader()
#our loop to check each line inside of our csv file is equal to what is inside the delete entry
for line in contents:
if line['employee id'] != str(delete_entry.get()):
csv_writer1.writerow(line)
答案 1 :(得分:0)
正如已经说过的那样。不要将输入与输出混合。另外,请阅读有关DictReader
和DictWrite
工作原理的文档:
https://docs.python.org/2/library/csv.html#csv.DictReader
https://docs.python.org/2/library/csv.html#csv.DictWriter
现在正在使用代码:
def delete_information_method():
# First, read input
new_records = []
with open('employees.csv', 'r') as emp_read:
csv_dictreader = csv.DictReader(emp_read)
for record in csv_dictreader:
# Note: you do not need to mark delete_entry as global
# Since you are only using it to get and not updating it
if record['employee id'] != str(delete_entry):
new_records.append(record)
# Now you have all records you care about and now you can
# write it into output
#
# BTW, since it is different block than
# with open('employees.csv'...
# There you can safely use same file name if you want/need
with open('output.csv', 'w') as emp_writer:
csv_writer = csv.DictWriter(
emp_writer,
# This is important since you are using dicts to
# work with csv
fieldnames=csv_dictreader.fieldnames
)
# Do not forget header row
csv_writer.writeheader()
for record in new_records:
csv_writer.writerow(record)