用Python代码自动生成文献的IEEE引用格式

今天尝试着将引用文献的格式按照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”
在这里插入图片描述
跳转到以下页面,复制所有字符串
在这里插入图片描述
运行我们上面给出的代码,在交互窗口把我们复制的字符串粘贴过去:
在这里插入图片描述
之后点击两下回车,即可得到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.

  • 78
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
IEEE 引用格式是一种常用的学术论文引用格式,适用于电子与电气工程领域的研究。在 LaTeX 中,我们可以使用 `IEEEtran` 文档类和 `IEEEtran.bst` 参考文献样式来实现 IEEE 引用格式。 首先,在 LaTeX 文档的导言区,我们需要指定文档类为 `IEEEtran`,即 `\documentclass{IEEEtran}`。然后,在正文中,我们可以使用 `\cite{}` 命令来引用参考文献,其中括号中填写参考文献在 .bib 文件中的标识符。例如,`\cite{article}` 表示引用一个标识符为 "article" 的参考文献。 为了生成符合 IEEE 引用格式参考文献列表,我们还需在文献引用命令之前插入一个 `\bibliography{}` 命令,其中括号内填写 .bib 文件的文件名。最后,在文档的结尾处添加一个 `\bibliographystyle{IEEEtran}` 命令,以指定参考文献样式IEEEtran。 接下来,我们需要准备一个 .bib 文件,其中包含我们引用参考文献的信息。每个参考文献条目都以一个特定的格式添加到 .bib 文件中。例如,对于期刊文章,我们可以使用 `@article` 类型的条目,并提供作者、标题、期刊、年份等信息。类似地,还有 `@book`、`@conference`、`@inproceedings` 等不同类型的条目可供选择。 最后,我们需要编译 LaTeX 文档以生成正确的引用格式。通常,我们需要连续运行 LaTeXBibTeX 编译命令,以确保参考文献正确引用并生成正确的参考文献列表。具体来说,我们可以使用以下命令顺序运行: 1. `pdflatex document.tex` 2. `bibtex document.aux` 3. `pdflatex document.tex` 4. `pdflatex document.tex` 以上就是使用 LaTeX 实现 IEEE 引用格式的基本步骤。通过正确配置文档类、参考文献样式和 .bib 文件,我们可以轻松生成符合 IEEE 引用格式的学术论文

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白水baishui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值