第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

通過 Python 鏈接到 csv 文件中的超鏈接

通過 Python 鏈接到 csv 文件中的超鏈接

牧羊人nacy 2022-10-18 15:18:38
如何使 CSV 文件中的所有鏈接都指向超鏈接?- 只需將 HTML 標(biāo)簽添加到所有帶有鏈接的字段我認(rèn)為可以通過 PythonCSV 字符串看起來像"Name","CompanyName","Value1","Value2","Value3","Value4","Value5","Value6","Value7","Url","Value8 ""Value8.1""""Name","Company1","SomeValue1","SomeValue2","SomeValue3","SomeValue4","SomeValue5","SomeValue6","SomeValue7","https://company1.com","Some"Value8 ""Value8.1""""Name","Company2","SomeValue1","SomeValue2","SomeValue3","SomeValue4","SomeValue5","SomeValue6","SomeValue7","https://company2.com","Some"Value8 ""Value8.1"""應(yīng)該看起來像:"Name","CompanyName","Value1","Value2","Value3","Value4","Value5","Value6", "Value7","Url","Value8""Name","Company1","SomeValue1","SomeValue2","SomeValue3","SomeValue4","SomeValue5","SomeValue6","SomeValue7","<a href="https://company1.com">https://company1.com</a>","Some"Value8 ""Value8.1""""Name","Company2","SomeValue1","SomeValue2","SomeValue3","SomeValue4","SomeValue5","SomeValue6","SomeValue7","<a href="https://company2.com">https://company2.com</a>","Some"Value8 ""Value8.1""""Name","Company3","SomeValue1","SomeValue2","SomeValue3","SomeValue4","SomeValue5","SomeValue6","SomeValue7","<a href="https://company3.com">https://company3.com</a>","Some"Value8 ""Value8.1"""我嘗試了 awk 和 sed,但是當(dāng)我在文件中指定一列時(shí)會(huì)出錯(cuò)。我覺得腳本應(yīng)該找到所有鏈接并將其插入 HTML 標(biāo)簽。但我還不明白它是如何工作的
查看完整描述

1 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是一個(gè)快速而骯臟的例子,說明如何做到這一點(diǎn)。您可以在此處查看 csv 模塊的示例


您的示例數(shù)據(jù)有一些額外的空格 - 如果您的真實(shí)數(shù)據(jù)是這樣的,您可以使用該strip()方法刪除空格。


如果您的 URL 在同一列中不一致,那么這將不起作用,您將需要使用 regex 或 urllib 來識(shí)別它們。


import csv


count = 0

with open('test.csv', 'r') as f:

    csv_reader = csv.reader(f)

    with open('outfile.csv', 'w') as outfile:

    for row in csv_reader:

        row[0] = f'"{row[0]}'

        if count < 1:

            outfile.write('","'.join(row) + '"\n')

            count += 1

        else:

            row[-2] = f'<a href="{row[-2]}">{row[-2]}</a>'

            outfile.write('","'.join(row) + '"\n')


查看完整回答
反對(duì) 回復(fù) 2022-10-18
  • 1 回答
  • 0 關(guān)注
  • 308 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)