Python 在csv文件中写入嵌套字典,但每次在csv文件中都会重复/追加头
我是python的新手。我已经编写了在csv文件中附加嵌套字典的代码。但每次我运行代码时,标题也会被忽略 但是我需要,头应该只需要在csv文件中附加一次,而不是更多 你有没有建议对我有帮助的修改 提前谢谢Python 在csv文件中写入嵌套字典,但每次在csv文件中都会重复/追加头,python,python-3.x,Python,Python 3.x,我是python的新手。我已经编写了在csv文件中附加嵌套字典的代码。但每次我运行代码时,标题也会被忽略 但是我需要,头应该只需要在csv文件中附加一次,而不是更多 你有没有建议对我有帮助的修改 提前谢谢 CSV file output CSV文件截图 在while循环之前,检查文件是否存在;如果没有;创建带有标题的文件。在循环中只写行。我尝试了这个file.tell()方法,它工作得很好!!无论如何,谢谢你的回答 with open('Empl
CSV file output
CSV文件截图
在while循环之前,检查文件是否存在;如果没有;创建带有标题的文件。在循环中只写行。我尝试了这个file.tell()方法,它工作得很好!!无论如何,谢谢你的回答
with open('Employee Details.csv', 'a') as csvfile:
csv_columns = ['User ID', 'Name', 'Age', 'Occupation', 'Department', 'Salary', 'Address']
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
# using file.tell we can verify the position whether there is any content in the position by specifying the value is 0
if csvfile.tell() == 0:
writer.writeheader()
for key,value in user_details.items():
row = {'User ID': key}
row.update(value)
writer.writerow(row)
你认为
writer.writeheader()
可以做什么?您将以追加模式在a
中打开CSV文件-因此,您的输出将在已经存在的所有内容之后写入。请不要发布代码/数据/回溯的图像。只需复制文本,粘贴到问题中,并将其格式化为代码。。数据的一个最小示例。
import csv
user_details = {}
while True:
user_input = input(" You're Operation Please ( New / View ) Details : ").lower()
if user_input == 'new':
create_user_ID = input(" Enter the user ID : ")
user_details[create_user_ID] = {}
user_name = input(" Enter the user name : ")
user_details[create_user_ID]['Name'] = user_name
user_age = int(input(" Enter the Age : "))
user_details[create_user_ID]['Age'] = user_age
user_occupation = input(" Enter the users occupation : ")
user_details[create_user_ID]['Occupation'] = user_occupation
user_department = input(" user department : ")
user_details[create_user_ID]['Department'] = user_department
user_income = int(input(" Enter the salary details : "))
user_details[create_user_ID]['Salary'] = user_income
user_address = input(" Enter the Address details ")
user_details[create_user_ID]['Address'] = user_address
print(f" New User account {create_user_ID} has been successfully created")
# Need to clarify this step,but program is running without any issues
with open('Employee Details.csv', 'a') as csvfile:
csv_columns = ['User ID', 'Name', 'Age', 'Occupation', 'Department', 'Salary', 'Address']
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for key,value in user_details.items():
row = {'User ID': key}
row.update(value)
writer.writerow(row)
process = input(" Do you want to continue the Account creation process (YES / NO ) : ").lower()
if process == 'no':
break
elif user_input == 'view':
user_ID = input("Enter the user_ID : ")
print(user_details[user_ID])
break
else:
print(" Please enter the proper command to execute (new / view)")
for detail in user_details.items():
print(detail)
with open('Employee Details.csv', 'a') as csvfile:
csv_columns = ['User ID', 'Name', 'Age', 'Occupation', 'Department', 'Salary', 'Address']
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
# using file.tell we can verify the position whether there is any content in the position by specifying the value is 0
if csvfile.tell() == 0:
writer.writeheader()
for key,value in user_details.items():
row = {'User ID': key}
row.update(value)
writer.writerow(row)