繁体   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