简体   繁体   中英

add url to list of webpages in csv using python

I Have a csv file containing the following data:
PageName, ID
Index, 1
Images, 2
Sounds, 3
Applications, 4
Games, 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 ").

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

Update 2:
My bad. haven't formatted the code properly (according to @Linuxios's answer).
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:

writer.writerow(reader.next())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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