我有一個帶有 URL 的 csv,其中包含我需要提取的資料。有時,URL 不包含或包含多個結果,如果是這種情況,我想將 a 添加None到串列中。
這是代碼:
import os
import glob
import time
from urllib.request import urlopen
from numpy import full
import pandas as pd
import xml.etree.ElementTree as ET
count=0
files=glob.glob('./extract/isbnlist/Reihe*_isbn-dnb21.csv',recursive=True) #searches all files in folder
print(files)
for file in files:
if count==0: #to only go through the first file, instead of all files in the folder
csvfile = pd.read_csv(file, sep='\t', encoding='utf-8')
clean_aut = []
title = []
isbn_clean = []
for row in csvfile['URL']:
#print('row: ' row)
with urlopen(str(row)) as response:
doc = ET.parse(response)
root = doc.getroot()
namespaces = { # Manually extracted from the XML file, but there could be code written to automatically do that.
"zs": "http://www.loc.gov/zing/srw/",
"": "http://www.loc.gov/MARC21/slim",
}
datafield_nodes_path = "./zs:records/zs:record/zs:recordData/record/datafield" # XPath
datafield_attribute_filters = [ #which fields to extract
{
"tag": "100", #author
"ind1": "1",
"ind2": " ",
}]
#datafield_attribute_filters = [] # Decomment this line to clear filters (and process each datafield node)
for datafield_node in root.iterfind(datafield_nodes_path, namespaces=namespaces):
if datafield_attribute_filters:
skip_node = True
for attr_dict in datafield_attribute_filters:
for k, v in attr_dict.items():
if datafield_node.get(k) != v:
break
else:
skip_node = False
break
if skip_node:
continue
for subfield_node in datafield_node.iterfind("./subfield[@code='a']", namespaces=namespaces):
clean_aut.append(subfield_node.text) #this gets the author name and title
origdata=pd.DataFrame({'Author':clean_aut})
print(clean_aut)
print(origdata)
count =1
這是帶有 URL 的串列檔案:Pastebin
我該怎么做?
uj5u.com熱心網友回復:
你應該在發布之前清理你的代碼。最好堅持一個最小的可重現示例。在您的情況下,您可以洗掉外回圈,以便我們可以專注于導致問題的部分。
關于您的代碼:您應該在回圈之前設定在回圈期間未修改的變數。此外,檢查屬性可以any使您的代碼更輕。為了None在找不到作者時添加到您的串列中,您可以使用布林值:
namespaces = { # Manually extracted from the XML file, but there could be code written to automatically do that.
"zs": "http://www.loc.gov/zing/srw/",
"": "http://www.loc.gov/MARC21/slim",
}
datafield_nodes_path = "./zs:records/zs:record/zs:recordData/record/datafield" # XPath
datafield_attribute_filters = [ #which fields to extract
{
"tag": "100", #author
"ind1": "1",
"ind2": " ",
}]
clean_aut = []
for row in csvfile['URL']:
with urlopen(str(row)) as response:
doc = ET.parse(response)
root = doc.getroot()
no_aut = True
for datafield_node in root.iterfind(datafield_nodes_path, namespaces=namespaces):
if any(datafield_node.get(k) != v for attr_dict in datafield_attribute_filters for k,v in attr_dict.items()):
continue
for subfield_node in datafield_node.iterfind("./subfield[@code='a']", namespaces=namespaces):
clean_aut.append(subfield_node.text) #this gets the author name
no_aut = False
if no_aut: clean_aut.append(None)
origdata=pd.DataFrame({'Author':clean_aut})
print(clean_aut)
print(origdata)
輸出:
[None, 'Bergren, Lisa Tawn', 'Rahlwes, Ann-Kathrin', 'Ortner, Helmut', 'Ladwig-Winters, Simone', 'Huonker, Thomas', 'Ritter, Karl-Markus', 'Kerkeling, Hape', 'Rohls, Jan', 'Rohls, Jan', 'Rohls, Jan', 'James, Bethan', None, 'Schmidt, Horst']
Author
0 None
1 Bergren, Lisa Tawn
2 Rahlwes, Ann-Kathrin
3 Ortner, Helmut
4 Ladwig-Winters, Simone
5 Huonker, Thomas
6 Ritter, Karl-Markus
7 Kerkeling, Hape
8 Rohls, Jan
9 Rohls, Jan
10 Rohls, Jan
11 James, Bethan
12 None
13 Schmidt, Horst
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/466579.html
標籤:python-3.x 熊猫 CSV
上一篇:如何檢查Laravel中的最新行
