为 python(csv) 中列中的每个项目创建超链接
Create hyperlink for each item in column in python(csv)
我正在尝试为基于另一列的列中的每个项目创建超链接。
这里有一张图片可以帮助您更好地理解:
每个标题都应超链接到相应的 URL。 (当您点击苹果时,它应该转到 apple.com,当您单击香蕉时,它应该转到 banana.com,依此类推)有没有办法对 python 中的 CSV 文件执行此操作?
(假设我的数据是 2000 行)
提前致谢
您可以使用(我使用过)pandas
库从 csv 中读取数据,然后将其写入 excel,并利用 Excel 函数 HYPERLINK
使单元格成为超链接。 HYPERLINK
的 Excel 函数需要我们将要使用的 url(以 http:// 或 https:// 开头),然后是可见文本或友好名称。
打开 Excel 文件时,有一件事是超链接单元格的蓝色下划线文本。如果需要,您可以稍微利用此解决方案并使用 XlsxWriter 模块。
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)
没有用或者可能你包含了太多的
import pandas as pd
df = pd.read_csv('test.csv')
df['URL'] = 'https://url/'+df['id'].astype(str)
keep_col = ['URL']
newurl = df[keep_col]
newurl.to_csv("newurl.csv", index=False)
此代码有效,但输出文件未显示可点击的 url
我正在尝试为基于另一列的列中的每个项目创建超链接。
这里有一张图片可以帮助您更好地理解:
每个标题都应超链接到相应的 URL。 (当您点击苹果时,它应该转到 apple.com,当您单击香蕉时,它应该转到 banana.com,依此类推)有没有办法对 python 中的 CSV 文件执行此操作?
(假设我的数据是 2000 行)
提前致谢
您可以使用(我使用过)pandas
库从 csv 中读取数据,然后将其写入 excel,并利用 Excel 函数 HYPERLINK
使单元格成为超链接。 HYPERLINK
的 Excel 函数需要我们将要使用的 url(以 http:// 或 https:// 开头),然后是可见文本或友好名称。
打开 Excel 文件时,有一件事是超链接单元格的蓝色下划线文本。如果需要,您可以稍微利用此解决方案并使用 XlsxWriter 模块。
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)
没有用或者可能你包含了太多的
import pandas as pd
df = pd.read_csv('test.csv')
df['URL'] = 'https://url/'+df['id'].astype(str)
keep_col = ['URL']
newurl = df[keep_col]
newurl.to_csv("newurl.csv", index=False)
此代码有效,但输出文件未显示可点击的 url