简体   繁体   中英

Open link from csv using python, issue with invalid characters for a URL

This is the code I have, it works for the most part, it opens the url but the url contains '' which is invalid when trying to open a link. how do I remove these before using webbrowser.open?

import time, csv, webbrowser


wait_time = 30

with open(r'C:\Users\Documents\list_of_url.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        list2 = str(row).strip('[]')
        webbrowser.open(list2,new = 0, autoraise = True)

        time.sleep(wait_time)

example urls from csv https://1178/cr_1178_1.tif https://1179/cr_1179_1.tif https://1180/cr_1180_1.tif

Problem: This is what the url that it tries to open looks like, there are invalid characters ('') that a browser doesn't allow 'https//1178/cr_1178_1.tif'

SOLVED:

import time, csv, webbrowser


wait_time = 5

with open(r'D:\Book3.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        webbrowser.open(row[0], new = 0, autoraise = True)
        print(row)
        time.sleep(wait_time)

Solved:

import time, csv, webbrowser

wait_time = 5

with open(r'D:\Book3.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        webbrowser.open(row[0], new = 0, autoraise = True)
        print(row)
        time.sleep(wait_time)

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