【问题标题】:How to Extract the result from python into a xls file如何将python的结果提取到xls文件中
【发布时间】:2020-08-06 07:50:56
【问题描述】:

我是 python 新手,我需要从科学文献中提取参考资料。以下是我正在使用的代码

from refextract import extract_references_from_url
references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
print(references)

所以,请指导我如何将此打印的信息提取到 Xls 文件中。非常感谢。

【问题讨论】:

    标签: python xlsx xls python-3.8


    【解决方案1】:

    您可以使用 pandas 库将引用写入 excel。

    from refextract import extract_references_from_url
    import pandas as pd
    
    references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
    print(references)
    
    # convert to pandas dataframe
    dfref = pd.DataFrame(references)
    
    # write dataframe into excel
    dfref.to_excel('./refs.xlsx')
    

    【讨论】:

    • 安装 xlrd 比安装 pandas 更需要。
    【解决方案2】:

    你应该看看 xlsxwriter,一个用于创建 excel 文件的模块。 您的代码可能如下所示:

    import xlsxwriter
    from refextract import extract_references_from_url
    workbook = xlsxwriter.Workbook('References.xlsx')
    worksheet = workbook.add_worksheet()
    
    references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
    
    row = 0
    col = 0
    
    worksheet.write(references)
    
    workbook.close
    

    (根据https://xlsxwriter.readthedocs.io/tutorial01.html修改)

    【讨论】:

    • 试过但它给了我这个错误:TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
    【解决方案3】:

    浏览了refextract here的文档后,我发现你的变量references是一个字典。要将这样的字典转换为 python,您可以按如下方式使用 Pandas-

    import pandas as pd
    # create a pandas dataframe using a dictionary
    df = pd.DataFrame(data=references, index=[0])
    # Take transpose of the dataframe 
    df = (df.T)
    # write the dictionary to an excel file
    df.to_excel('extracted_references.xlsx')
    

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 1970-01-01
      • 2015-05-24
      • 2022-01-05
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2012-11-13
      相关资源
      最近更新 更多