利用Python脚本将BibTeX自动转换为IEEE引用格式

一颗松果
一颗松果
发布时间:2026-10-14 | 6人浏览过
原创

本文介绍如何使用Python编写小程序,将学术文献的BibTeX格式自动转换为标准的IEEE引用格式。通过解析百度学术或谷歌学术导出的BibTeX数据,程序可处理会议论文与期刊论文,支持自动提取作者、标题、会议/期刊名、卷期号及页码等信息,并生成符合IEEE规范的引用字符串,有效避免手动排版错误。

Python BibTeX转IEEE格式自动化工具与使用教程

手动逐条调整文献引用格式既繁琐又容易出错,因此尝试使用Python编写一个小工具,根据BibTeX格式自动生成符合IEEE标准的引用格式。

核心代码实现

具体实现代码如下:


import re

def getIeeeJournalFormat(bibInfo):
  """
  生成期刊文献的IEEE引用格式:{作者}, "{文章标题}," {期刊名称}, vol. {卷数}, no. {编号}, pp. {页码}, {年份}.
  :return: {author}, "{title}," {journal}, vol. {volume}, no. {number}, pp. {pages}, {year}.
  """
  # 避免字典出现null值
  if "volume" not in bibInfo:
    bibInfo["volume"] = "null"
  if "number" not in bibInfo:
    bibInfo["number"] = "null"
  if "pages" not in bibInfo:
    bibInfo["pages"] = "null"

  journalFormat = bibInfo["author"] + \
      ", \"" + bibInfo["title"] + \
      ",\" " + bibInfo["journal"] + \
      ", vol. " + bibInfo["volume"] + \
      ", no. " + bibInfo["number"] + \
      ", pp. " + bibInfo["pages"] + \
      ", " + bibInfo["year"] + "."

  # 对格式进行调整,去掉没有的信息,调整页码格式
  journalFormatNormal = journalFormat.replace(", vol. null", "")
  journalFormatNormal = journalFormatNormal.replace(", no. null", "")
  journalFormatNormal = journalFormatNormal.replace(", pp. null", "")
  journalFormatNormal = journalFormatNormal.replace("--", "-")
  return journalFormatNormal

def getIeeeConferenceFormat(bibInfo):
  """
  生成会议文献的IEEE引用格式:{作者}, "{文章标题}, " in {会议名称}, {年份}, pp. {页码}.
  :return: {author}, "{title}, " in {booktitle}, {year}, pp. {pages}.
  """
  conferenceFormat = bibInfo["author"] + \
          ", \"" + bibInfo["title"] + ",\" " + \
          ", in " + bibInfo["booktitle"] + \
          ", " + bibInfo["year"] + \
          ", pp. " + bibInfo["pages"] + "."

  # 对格式进行调整,,调整页码格式
  conferenceFormatNormal = conferenceFormat.replace("--", "-")
  return conferenceFormatNormal

def getIeeeFormat(bibInfo):
  """
  本函数用于根据文献类型调用相应函数来输出ieee文献引用格式
  :param bibInfo: 提取出的BibTeX引用信息
  :return: ieee引用格式
  """
  if "journal" in bibInfo: # 期刊论文
    return getIeeeJournalFormat(bibInfo)
  elif "booktitle" in bibInfo: # 会议论文
    return getIeeeConferenceFormat(bibInfo)

def inforDir(bibtex):
  #pattern = "[\w]+={[^{}]+}"  用正则表达式匹配符合 ...={...} 的字符串
  pattern1 = "[\w]+=" # 用正则表达式匹配符合 ...= 的字符串
  pattern2 = "{[^{}]+}" # 用正则表达式匹配符合 内层{...} 的字符串

  # 找到所有的...=,并去除=号
  result1 = re.findall(pattern1, bibtex)
  for index in range(len(result1)) :
    result1[index] = re.sub('=', '', result1[index])
  # 找到所有的{...},并去除{和}号
  result2 = re.findall(pattern2, bibtex)
  for index in range(len(result2)) :
    result2[index] = re.sub('\{', '', result2[index])
    result2[index] = re.sub('\}', '', result2[index])

  # 创建BibTeX引用字典,归档所有有效信息
  infordir = {}
  for index in range(len(result1)):
    infordir[result1[index]] = result2[index]
  return infordir

def inputBibTex():
  """
  在这里输入BibTeX格式的文献引用信息
  :return:提取出的BibTeX引用信息
  """
  bibtex = []
  print("请输入BibTeX格式的文献引用:")
  i = 0
  while i < 15: # 观察可知BibTeX格式的文献引用不会多于15行
    lines = input()
    if len(lines) == 0: # 如果输入空行,则说明引用内容已经输入完毕
      break
    else:
      bibtex.append(lines)
    i += 1
  return inforDir("".join(bibtex))

if __name__ == '__main__':
  bibInfo = inputBibTex() # 获得BibTeX格式的文献引用
  print(getIeeeFormat(bibInfo)) # 输出ieee格式

工具使用步骤详解

以下是该代码的具体使用步骤说明。

首先,需要获取文献的BibTeX引用格式数据。这可以通过百度学术或谷歌学术等平台的引用功能获得。以谷歌学术为例进行演示:

在搜索框中输入论文标题:Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application,随后进入搜索结果页面:

点击“引用”按钮,接着选择“BibTex”选项。


页面跳转后,复制页面上显示的所有字符串内容。


运行前述编写的Python代码,在交互窗口中将刚才复制的字符串粘贴进去:


随后连续按下两次回车键,系统即可输出IEEE格式的文献引用结果:

常见文献格式转换示例

代码中区分了会议论文和期刊论文两种格式。若需其他引用格式,可基于此代码进行增删改。以下提供部分格式转换示例供参考:

会议论文案例1:

Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application

对应的BibTeX格式如下:

@inproceedings{hu2018reinforcement, title={Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application}, author={Hu, Yujing and Da, Qing and Zeng, Anxiang and Yu, Yang and Xu, Yinghui}, booktitle={Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining}, pages={368–377}, year={2018} }

转换后的IEEE格式为:

Hu, Yujing and Da, Qing and Zeng, Anxiang and Yu, Yang and Xu, Yinghui, “Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application,” , in Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining, 2018, pp. 368-377.

会议论文案例2:

A contextual-bandit approach to personalized news article recommendation

对应的BibTeX格式如下:

@inproceedings{li2010contextual, title={A contextual-bandit approach to personalized news article recommendation}, author={Li, Lihong and Chu, Wei and Langford, John and Schapire, Robert E}, booktitle={Proceedings of the 19th international conference on World wide web}, pages={661–670}, year={2010} }

转换后的IEEE格式为:

Li, Lihong and Chu, Wei and Langford, John and Schapire, Robert E, “A contextual-bandit approach to personalized news article recommendation,” , in Proceedings of the 19th international conference on World wide web, 2010, pp. 661-670.

期刊论文案例1:

Infrared navigation-Part I: An assessment of feasibility

对应的BibTeX格式如下:

@article{duncombe1959infrared, title={Infrared navigation—Part I: An assessment of feasibility}, author={Duncombe, JU}, journal={IEEE Trans. Electron Devices}, volume={11}, number={1}, pages={34–39}, year={1959} }

转换后的IEEE格式为:

Duncombe, JU, “Infrared navigation—Part I: An assessment of feasibility,” IEEE Trans. Electron Devices, vol. 11, no. 1, pp. 34-39, 1959.

期刊论文案例2(arXiv预印本):

Reinforcement learning for slate-based recommender systems: A tractable decomposition and practical methodology

对应的BibTeX格式如下:

@article{ie2019reinforcement, title={Reinforcement learning for slate-based recommender systems: A tractable decomposition and practical methodology}, author={Ie, Eugene and Jain, Vihan and Wang, Jing and Narvekar, Sanmit and Agarwal, Ritesh and Wu, Rui and Cheng, Heng-Tze and Lustman, Morgane and Gatto, Vince and Covington, Paul and others}, journal={arXiv preprint arXiv:1905.12767}, year={2019} }

转换后的IEEE格式为:

Ie, Eugene and Jain, Vihan and Wang, Jing and Narvekar, Sanmit and Agarwal, Ritesh and Wu, Rui and Cheng, Heng-Tze and Lustman, Morgane and Gatto, Vince and Covington, Paul and others, “Reinforcement learning for slate-based recommender systems: A tractable decomposition and practical methodology,” arXiv preprint arXiv:1905.12767, 2019.

关于使用Python代码自动生成文献IEEE引用格式的实现介绍至此。如需了解更多Python自动生成IEEE格式的相关内容,建议搜索本站过往文章或继续浏览后续内容,感谢支持。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表