简体   繁体   English

如何将 python 的结果提取到 xls 文件中

[英]How to Extract the result from python into a xls file

I'm a novice in python and I need to extract references from scientific literature.我是 python 的新手,我需要从科学文献中提取参考资料。 Following is the code I'm using以下是我正在使用的代码

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

So, Please guide me on how to extract this printed information into a Xls file.所以,请指导我如何将此打印信息提取到 Xls 文件中。 Thank you so much.太感谢了。

You could use the pandas library to write the references into excel.您可以使用 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')

You should have a look at xlsxwriter, a module for creating excel files.你应该看看 xlsxwriter,一个用于创建 excel 文件的模块。 Your code could then look like this:您的代码可能如下所示:

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

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

After going through the documentation of refextract here , I found that your variable references is a dictionary.在浏览了 refextract here的文档后,我发现您的变量references是一个字典。 For converting such a dictionary to python you can use Pandas as follows-要将这样的字典转换为 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM