简体   繁体   English

使用python将URL添加到csv中的网页列表

[英]add url to list of webpages in csv using python

I Have a csv file containing the following data: 我有一个包含以下数据的csv文件:
PageName, ID PageName,ID
Index, 1 索引1
Images, 2 图片,2
Sounds, 3 声音3
Applications, 4 应用程序,4
Games, 5 游戏,5
... ...

I want to add a prefixed url, to each of the pages listed under "PageName" (Example, making "Index", to be " https://www.example.com/Index "). 我想在“ PageName”下列出的每个页面上添加一个前缀URL(例如,将“ Index”设置为“ https://www.example.com/Index ”)。

Update: 更新:
I've tried to use the following code to do so: 我尝试使用以下代码进行操作:

import csv
reader = csv.reader(open('small.csv', 'rb'))
writer = csv.writer(open('full.csv', 'wb'))
for row in reader:
    writer.writerow("https://www.example.com/" + [row[0], row[1]])

But i get the error: 但是我得到了错误:
writer.writerow(" https://www.example.com/ " + [row[0], row[1]]) TypeError: cannot concatenate 'str' and 'list' objects writer.writerow(“ https://www.example.com/ ” + [row [0],row [1]])TypeError:无法连接'str'和'list'对象

Update 2: 更新2:
My bad. 我的错。 haven't formatted the code properly (according to @Linuxios's answer). 没有正确格式化代码(根据@Linuxios的答案)。
now everything is working. 现在一切正常。

Try this: 尝试这个:

writer.writerow(["https://www.example.com/" + row[0], row[1]])

To prevent this from also prepending the URL to the header, add this before the loop to pass the headers through unchanged: 为了防止此操作将URL放在标题之前,请将其添加到循环之前,以使标题通过不变:

writer.writerow(reader.next())

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

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