Reputation: 726
I am relatively new to Python, with slightly more that a year of programming experience with R.
I am trying to write code that helps me update specific fields in my Zotero library so that they conform to citation standards.
I noticed that the author
field in Zotero can be mapped to various items in the dictionary object.
from pyzotero import zotero as z
zot = z.Zotero(library_id, library_type, api_key, preserve_json_order=True)
zot.collections()
song_historiography=zot.collection_items('GLN5VY3Z')
x=int()
song_historiography[x]['data']['creators']
By varying the values of x
, I get to see the different data structures under which an author's name is stored.
[{'creatorType': 'author', 'name': '舒仁輝'}]
[{'creatorType': 'author', 'firstName': 'On Cho', 'lastName': 'Ng'},
{'creatorType': 'author', 'firstName': 'Q. Edward', 'lastName': 'Wang'}]
[{'creatorType': 'author', 'firstName': 'Peter K.', 'lastName': 'Bol'},
{'creatorType': 'editor',
'firstName': 'Dieter Kuhn',
'lastName': 'Helga Stahl'}]
How do we access the name
, firstName
and lastName
fields for the collection as a whole (collectively and/or separately) so that changes can be made to them directly via code?
Upvotes: 0
Views: 261
Reputation: 1779
I think DataFrame
of pandas
is good solution for you.
import pandas as pd
First of all, I merged data seperated in 1 list.
data = [
[{'creatorType': 'author', 'name': '舒仁輝'}],
[{'creatorType': 'author', 'firstName': 'On Cho', 'lastName': 'Ng'},
{'creatorType': 'author', 'firstName': 'Q. Edward', 'lastName': 'Wang'}],
[{'creatorType': 'author', 'firstName': 'Peter K.', 'lastName': 'Bol'},
{'creatorType': 'editor', 'firstName': 'Dieter Kuhn', 'lastName': 'Helga Stahl'}]
]
authors = []
for d in data: authors += d
print (authors)
[{'creatorType': 'author', 'name': '舒仁輝'}, {'creatorType': 'author', 'firstName': 'On Cho', 'lastName': 'Ng'}, {'creatorType': 'author', 'firstName': 'Q. Edward', 'lastName': 'Wang'}, {'creatorType': 'author', 'firstName': 'Peter K.', 'lastName': 'Bol'}, {'creatorType': 'editor', 'firstName': 'Dieter Kuhn', 'lastName': 'Helga Stahl'}]
Created DataFrame from authors
df = pd.DataFrame(authors)
print (df)
creatorType firstName lastName name
0 author NaN NaN 舒仁輝
1 author On Cho Ng NaN
2 author Q. Edward Wang NaN
3 author Peter K. Bol NaN
4 editor Dieter Kuhn Helga Stahl NaN
And, I made an example to set value to specific item.
df.at[df.name == '舒仁輝','firstName'] = 'John'
df.at[df.firstName.str.contains('Cho'), 'creatorType'] = 'editor'
print (df)
creatorType firstName lastName name
0 author John NaN 舒仁輝
1 editor On Cho Ng NaN
2 author Q. Edward Wang NaN
3 author Peter K. Bol NaN
4 editor Dieter Kuhn Helga Stahl NaN
Upvotes: 1