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

简介: 用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引用格式,可以在百度学术,或者谷歌学术的应用栏中找到,例如这里以谷歌学术举例:


0a2653c851af460fa595bd959398a8f1.png


在搜索框搜索论文:Reinforcement learning to rank in e-commerce search engine: Formalization, analysis, and application,跳转到以下页面:


2d65d23f6d4748949b924e4057485923.png


点击“引用”,再点击“BibTex”


6de278e6d6694ce5bb08e7e842b7e74b.png


跳转到以下页面,复制所有字符串


8ec4f2997fb246878c34ecd6d122b7c6.png


运行我们上面给出的代码,在交互窗口把我们复制的字符串粘贴过去:

12c3b7f3f8814309a195c64f051d4445.png

之后点击两下回车,即可得到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.
相关文章
|
1天前
|
数据采集 JSON API
【2024-简洁版】python爬虫 批量查询自己所有CSDN文章的质量分:方便快速上手修改代码
【2024-简洁版】python爬虫 批量查询自己所有CSDN文章的质量分:方便快速上手修改代码
8 0
|
1天前
|
Python
Python中的装饰器:提升代码可读性与复用性
Python中的装饰器是一种强大的工具,能够提升代码的可读性和复用性。本文将深入探讨装饰器的原理、用法以及在实际项目中的应用,帮助读者更好地理解和利用这一特性,提升代码质量和开发效率。
|
2天前
|
监控 Python
Python中的装饰器:提升代码可读性与可维护性
Python中的装饰器是一种强大的工具,可以在不修改函数源代码的情况下,增加新的功能。本文将介绍装饰器的基本概念,以及如何使用装饰器来提升代码的可读性和可维护性。通过实例演示,读者将了解装饰器在各种场景下的灵活运用,从而更好地理解并应用于实际开发中。
|
2天前
|
缓存 Python
Python中的装饰器:提升代码可读性与灵活性
在Python编程中,装饰器是一种强大的工具,可以通过在函数或方法周围包装额外的功能来提升代码的可读性和灵活性。本文将深入探讨装饰器的概念、用法和实际应用,帮助读者更好地理解并运用这一Python编程的利器。
|
3天前
|
缓存 并行计算 Serverless
优化Python代码性能的5个技巧
在日常Python编程中,代码性能的优化是一个重要的议题。本文介绍了5个实用的技巧,帮助你提高Python代码的执行效率,包括使用适当的数据结构、优化循环结构、利用内置函数、使用生成器表达式以及并行化处理。通过这些技巧,你可以更高效地编写Python代码,提升程序的性能和响应速度。
|
4天前
|
Python
探索Python中的装饰器:提升代码灵活性与可维护性
Python中的装饰器是一种强大的工具,可以在不改变原有代码结构的情况下,动态地添加功能或修改函数的行为。本文将深入探讨装饰器的原理、常见用法以及如何利用装饰器提升代码的灵活性和可维护性。
|
5天前
|
机器学习/深度学习 自然语言处理 算法
Python遗传算法GA对长短期记忆LSTM深度学习模型超参数调优分析司机数据|附数据代码
Python遗传算法GA对长短期记忆LSTM深度学习模型超参数调优分析司机数据|附数据代码
|
5天前
|
数据可视化 Python
python中Copula在多元联合分布建模可视化2实例合集|附数据代码
python中Copula在多元联合分布建模可视化2实例合集|附数据代码
|
5天前
|
人工智能 Python
Python中的反对称矩阵:理论、应用与代码实践
Python中的反对称矩阵:理论、应用与代码实践
23 1
|
5天前
|
SQL 数据库 数据库管理
python自动生成SQL语句自动化
python自动生成SQL语句自动化
23 1