我正在嘗試創建一個從 xml 創建 2 個 csv 檔案的程式。但是,我的資料沒有寫入 csv 檔案。我設法只得到了標題。
- 每年出版多少本書。
- 每個主題標題被提及多少次。
這是我正在使用的 xml 示例
<records>
<rec resultID="1">
<header shortDbName="cat01806a" longDbName="Simmons Library Catalog" uiTerm="sim.b2083905">
<controlInfo>
<bkinfo>
<btl>Android programming [electronic resource] : pushing the limits / Erik Hellman.</btl>
<isbn type="print">9781118717301</isbn>
<isbn type="print">9781118717356</isbn>
</bkinfo>
<jinfo />
<pubinfo>
<dt year="2014" month="01" day="01"></dt>
</pubinfo>
<artinfo>
<tig>
<atl>Android programming. [electronic resource] : pushing the limits.</atl>
</tig>
<aug>
<au>Hellman, Erik</au>
</aug>
<sug>
<subj type="unclass">Android (Electronic resource)</subj>
<subj type="unclass">Application software -- Development</subj>
<subj type="unclass">Smartphones -- Programming</subj>
<subj type="unclass">Tablet computers -- Programming</subj>
</sug>
<pubtype>eBook</pubtype>
<pubtype>Book</pubtype>
<doctype>Bibliographies</doctype>
<formats />
</artinfo>
<language>English</language>
</controlInfo>
<displayInfo>
<pLink>
<url>http://ezproxy.simmons.edu:2048/login?url=https://search.ebscohost.com/login.aspx?direct=true&db=cat01806a&AN=sim.b2083905&site=eds-live&scope=site</url>
</pLink>
</displayInfo>
</header>
</rec>
這是我到目前為止的代碼:
#import libraries
import csv, xml
import xml.etree.ElementTree as ET
#read open
base = ET.parse('simmons_program_books.xml')
detail = base.getroot()
#frequeny count for dictionary
def count(dictionary, key):
if key in dictionary:
dictionary[key] = 1
else:
dictionary[key] = 1
#empty dictionary variables
year_count = {}
subhead_count = {}
for year in detail.iter('dt year'):
#variable
count(year_count, year.text)
for subhead in detail.iter('subj type'):
count(subhead_count, subhead.text)
#to a csv (year)
year = open("year.csv", mode ='w', newline = '', encoding="utf-8")
write = csv.writer(year)
write.writerow(['year', '# books'])
for x, z in year_count.items():
write.writerow([x, z])
#close
year.close()
#to a csv (subhead)
subhead = open("subhead.csv", mode = 'w', newline = '', encoding ="utf-8")
write = csv.writer(subhead)
write. writerow(['subheading', '# amt mentioned'])
for x, z in subhead_count.items():
write.writerow([x, z])
#close
subhead.close()
我不確定出了什么問題。
uj5u.com熱心網友回復:
您的
iter()方法正在尋找不存在的孩子'dt year'&'subj type'。他們應該尋找,'year'而'subj'不是。要填充字典中的年份文本,請使用
year.get('year')代替year.text。
uj5u.com熱心網友回復:
首先,detail.iter('dt year')不會作業。迭代dts 然后檢查年份。
其次,你的計數函式必須回傳一些東西
#frequeny count for dictionary
def count(dictionary, key):
if key in dictionary:
dictionary[key] = 1
else:
dictionary[key] = 1
return dictionary
#empty dictionary variables
year_count = {}
subhead_count = {}
for dt in detail.iter('dt'):
#variable
year_count=count(year_count, dt.attrib['year'])
print('year', dt, dt.attrib['year'])
for subhead in detail.iter('subj'):
subhead_count=count(subhead_count, subhead.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351635.html
上一篇:在匯出到同一CSV和從同一CSV匯入的兩個資料框中找到不同的值
下一篇:Java供應商<>獲取來源資訊
