簡體   English   中英

數據未寫入 csv 文件

[英]Data not writing in csv file

我正在嘗試創建一個從 xml 創建 2 個 csv 文件的程序。 但是,我的數據沒有寫入 csv 文件。 我設法只得到了標題。

  1. 每年出版多少本書
  2. 每個主題標題被提及多少次

這是我正在使用的 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&amp;db=cat01806a&amp;AN=sim.b2083905&amp;site=eds-live&amp;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()

我不確定出了什么問題。

  1. 您的iter()方法正在尋找不存在的孩子'dt year' & 'subj type' 他們應該尋找'year''subj'

  2. 要填充字典中的年份文本,請使用year.get('year')而不是year.text

首先, detail.iter('dt year')不起作用。 迭代dt s,然后檢查年份。

其次,你的計數函數必須返回一些東西

#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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM