2017-03-27 16 views
0

我已經創建了一個員工數據庫的n個用戶輸入與IDNO,名稱,職位和工資等各種細節。員工數據庫完全在蟒蛇但是當我嘗試把它寫爲CSV詞典的字典對csv的用戶輸入

這裏是我的代碼:

import csv 
d={} 
d1={} 

n=int(raw_input("Enter the number of employees :")) 
for i in range(n): 
    emp_name=raw_input("Enter the name of employee :") 
    emp_idno=raw_input("Enter the idno of employee :") 
    emp_position=raw_input("Enter the position of employee :") 
    emp_salary=int(raw_input("Enter the salary of employee :")) 

    d1={emp_idno: {"Name": emp_name, "Position":emp_position, "Salary":emp_salary}} 
    d.update(d1) 
    print d 

for key,value in d.iteritems(): 
    print key,value 

    with open('12file1.csv','w') as f: 
     w = csv.writer(f) 
     w.writerow(['Idno','Name','Position', 'Salary']) 
     w.writerows(d.iteritems()) 

with open('12file1.csv','w') as f: 
    w = csv.writer(f) 
    w.writerow(['Idno','Name','Position', 'Salary']) 
    for keys, values in d.iteritems(): 
     w.writerows({'Idno':key,'Name':value['Name'],'Position':value["Position"], 'Salary':value["Salary"]}) 

我的字典的字典應該像

{idno: {name,position,salary}} 

我試着迭代字典的字典,然後寫入csv。如果我能以更簡單的方式將嵌套字典編寫爲csv,那將是一件好事。

我的CSV輸出應該

Idno Name Position Salary 
101  Abc  Trainee  12000 
102  Def  Fresher  8000 
+0

時需要添加ID號。您擁有的代碼(第二個循環)是否會產生不正確的結果?對我來說看起來很好。 – jknupp

回答

1

兩點

  • 在其中你寫你的價值觀行,

    w.writerows({'Idno':key,'Name':value['Name'],'Position':value["Position"], 'Salary':value["Salary"]}) 
    

變量的循環使用是values和你認爲的那個ESS裏面value

{'Idno':keys,'Name':values['Name'],'Position':values["Position"], 'Salary':values["Salary"]} 
  • 編寫詞典到CSV,

製作使用Python csv.DictWriter()來寫你頭。

with open('12file1.csv','w') as f: 
    w = csv.DictWriter(f,fieldnames=['Idno','Name','Position', 'Salary']) 
    w.writeheader() 
    for keys,values in d.iteritems(): 
     w.writerow({'Idno':keys,'Name':values['Name'],'Position':values["Position"], 'Salary':values["Salary"]})      

也就是說,

import csv 
d={} 
d1={} 

n=int(raw_input("Enter the number of employees :")) 
for i in range(n): 
    emp_name=raw_input("Enter the name of employee :") 
    emp_idno=raw_input("Enter the idno of employee :") 
    emp_position=raw_input("Enter the position of employee :") 
    emp_salary=int(raw_input("Enter the salary of employee :")) 

    d1={emp_idno: {"Name": emp_name, "Position":emp_position, "Salary":emp_salary}} 
    d.update(d1) 
    print d 

for key,value in d.iteritems(): 
    print key,value 

with open('12file1.csv','w') as f: 
    w = csv.DictWriter(f,fieldnames=['Idno','Name','Position', 'Salary']) 
    w.writeheader() 
    for keys,values in d.iteritems(): 
     w.writerow({'Idno':keys,'Name':values['Name'],'Position':values["Position"], 'Salary':values["Salary"]})      

希望它能幫助!

1

當您使用字典時,使用csv.DictWriter()來處理輸出CSV文件會更有意義。使用CSV庫時,應始終以二進制模式打開文件,因此要編寫您需要的格式爲wb,否則最終可能會在文件中出現多餘的空行。

下應該給你你想要的東西:

import csv 

d = {} 
d1 = {} 

n = int(raw_input("Enter the number of employees: ")) 

for i in range(n): 
    emp_name = raw_input("Enter the name of employee: ") 
    emp_idno = raw_input("Enter the idno of employee: ") 
    emp_position = raw_input("Enter the position of employee: ") 
    emp_salary = int(raw_input("Enter the salary of employee: ")) 

    d1 = {emp_idno: {"Name": emp_name, "Position":emp_position, "Salary":emp_salary, "Idno":emp_idno}} 
    d.update(d1) 
    print 

with open('12file1.csv','wb') as f: 
    w = csv.DictWriter(f, fieldnames=['Idno', 'Name', 'Position', 'Salary']) 
    w.writeheader() 

    for idno, employee in d.iteritems(): 
     w.writerow(employee) 

注意,因爲你是保存條目字典,訂單將不會被保留。使用詞典列表而不是詞典詞典可能更有意義。請注意,我在字典中添加了ID號作爲關鍵字,否則在執行writerow()