首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为python(csv)中的列中的每个项目创建超链接

为python(csv)中的列中的每个项目创建超链接
EN

Stack Overflow用户
提问于 2021-05-25 07:18:38
回答 1查看 169关注 0票数 1

我正在尝试为基于另一列的列中的每个项目创建一个超链接。

这里有一张图片,可以帮助你更好地理解:

每个标题都应该超链接到相应的URL。(当你点击苹果时,它应该转到apple.com,当你点击香蕉时,它应该转到banana.com,依此类推)有没有办法在python中对CSV文件执行此操作?

(假设我的数据是2000行)

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-25 10:12:38

您可以使用(我用过的) pandas库从csv中读取数据,然后将其写入excel,然后利用HYPERLINK函数将单元格变成一个超链接。HYPERLINK的Excel函数需要我们要转到的url (在开头带有http://或https:// ),然后是可见文本或友好名称。

当您打开Excel文件时,它不会包含超链接单元格的蓝色下划线文本。如果需要,您可以在一定程度上利用此解决方案,还可以使用XlsxWriter模块。

代码语言:javascript
复制
import pandas as pd

df = pd.read_csv("path\test.csv") #put your actual path here
# this will read the file and save it is a 'dataframe', basically a table.
df['title'] = '=HYPERLINK("https://' + df["url"] +'","' + df["title"]+'")' 
"""the df that we originally pulled in has the columns 'title' and 'url'. This line re-writes the values in 
'title' to use the HYPERLINK formula in Excel, where the link is the value from the URL column with 'https://'
added to the beginning, and then uses the value from 'title' as the text the user will see in Excel"""
df.to_excel("save_location\test2.xlsx",index=False) #this saves the new file as an Excel. index=False removes the index column that was created when you first make any dataframe

如果您希望您的输出只有一列,那么最后一行将略有不同:df['title'].to_excel("save_location\test2.xlsx",index=False)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67679916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档