Python Citeproc: ошибка с обновленным apa.csl, отлично работает со старым apa.cslPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Python Citeproc: ошибка с обновленным apa.csl, отлично работает со старым apa.csl

Сообщение Anonymous »

Я использую библиотеку citeproc-py для создания цитат в стиле APA в Python. Он отлично работает со старой версией apa.csl, но когда я перехожу на обновленную версию (скачанную из репозитория CSL GitHub), Старый CSL
Как решить эту проблему.
Что я пробовал:
Тестирование старого apa.csl: он работает отлично, без ошибок.
import requests
import json
from pybtex.database import parse_string
from citeproc import CitationStylesStyle, CitationStylesBibliography
from citeproc import Citation, CitationItem
from citeproc import formatter
from citeproc.source.json import CiteProcJSON

# Function to fetch metadata using DOI from CrossRef in BibTeX format
def fetch_metadata_from_doi(doi):
url = f"https://api.crossref.org/works/{doi}/tr ... n/x-bibtex"
headers = {"Accept": "application/x-bibtex"}
response = requests.get(url, headers=headers)

if response.status_code == 200:
return response.text
else:
print(f"Error: Unable to fetch metadata for DOI {doi}")
print(f"HTTP Status Code: {response.status_code}")
print(f"Response Content: {response.text[:500]}") # Print the first 500 characters of the response for debugging
return None

# Convert BibTeX to CSL format
def convert_bibtex_to_csl(bibtex_data):
# Parse the BibTeX string using pybtex
bib_data = parse_string(bibtex_data, bib_format='bibtex')

# Prepare the CSL format structure
bib_entries = []

for entry in bib_data.entries.values():
# Handle authors
authors = []
for author in entry.persons.get("author", []):
given_names = ' '.join(author.first_names) if author.first_names else ''
family_name = ' '.join(author.last_names) if author.last_names else ''
authors.append({"family": family_name, "given": given_names})

# Create CSL entry
bib_entry = {
"id": entry.key,
"type": "book", # Assuming the DOI is for a book, adjust for articles as needed
"title": entry.fields.get("title", ""),
"author": authors,
"issued": {
"date-parts": [[int(entry.fields.get("year", 0)), 1, 1]] # Default to January 1 of the year
},
"publisher": entry.fields.get("publisher", ""),
"DOI": entry.fields.get("doi", "")
}
bib_entries.append(bib_entry)

return bib_entries

# Example DOI for a book
doi = "10.1016/j.jcss.2024.103615" # Replace with the actual DOI you want to fetch

# doi = "978113806512"
# Initialize the variable to ensure it's always defined
bib_entries_doi = []

# Fetch metadata for DOI
bibtex_data_doi = fetch_metadata_from_doi(doi)
if bibtex_data_doi:
bib_entries_doi = convert_bibtex_to_csl(bibtex_data_doi)

# If no metadata is found, inform the user
if not bib_entries_doi:
print(f"No metadata found for DOI {doi}. Please check the DOI or the CrossRef API response.")

# Convert to CiteProc JSON if entries are available
if bib_entries_doi:
bib_source = CiteProcJSON(bib_entries_doi)

# Load the APA CSL style
bib_style = CitationStylesStyle('apa', validate=False)

# Create the citeproc-py bibliography
bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)

# Dynamically create citation objects for each reference in the BibTeX data
citations = []
for entry in bib_entries_doi:
citation_item = CitationItem(entry["id"])
citation = Citation([citation_item])
citations.append(citation)

# Register all citations with the bibliography
for citation in citations:
bibliography.register(citation)

# Define the warning function
def warn(citation_item):
print(f"WARNING: Reference with key '{citation_item.key}' not found in the bibliography.")

# Generate citations for all registered references
print('Citations')
print('---------')
for citation in citations:
print(bibliography.cite(citation, warn))

# Finally, render the bibliography
print('')
print('Bibliography')
print('------------')
for item in bibliography.bibliography():
print(str(item))



Подробнее здесь: https://stackoverflow.com/questions/793 ... ld-apa-csl
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Запуск Cordova всегда дает ошибку со всем обновленным
    Anonymous » » в форуме JAVA
    0 Ответы
    23 Просмотры
    Последнее сообщение Anonymous
  • Как повторно реализовать Avcodec с обновленным Avcodeccontext on Fly
    Anonymous » » в форуме C++
    0 Ответы
    12 Просмотры
    Последнее сообщение Anonymous
  • Как я могу перезаписать и существующий список с обновленным? [дублировать]
    Anonymous » » в форуме Python
    0 Ответы
    10 Просмотры
    Последнее сообщение Anonymous
  • Прочтите файл с обновленным контентом 5 раз [закрыто]
    Anonymous » » в форуме C++
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous
  • Веб-сайт Python Flask Ubuntu Apache2, обслуживающий изображения за пределами DocumentRoot с использованием alias_mod apa
    Anonymous » » в форуме Apache
    0 Ответы
    63 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»