-2

I Have a list of string(which are actually URL)

['https://part.of.url/P2000-01.xls',

 'https://part.of.url/P2001-02.xls',

 'https://part.of.url/P2002-03.xls',

 'https://part.of.url/P2003-04.xls',

 'https://part.of.url/P2004-05.xls',

 'https://part.of.url/P2005-06.xls',

 'https://part.of.url/P2006-07.xls',

 'https://part.of.url/P2007-08.xls',

 'https://part.of.url/P2008-09.xls',

 'https://part.of.url/P2009-10.xls',

 'https://part.of.url/P2010-11.xls',

 'https://part.of.url/P2011-12.xls',

 'https://part.of.url/P2012-13.xls',

 'https://part.of.url/P2013-14.xls',

 'https://part.of.url/P2014-15.xls',

 'https://part.of.url/P2015-16.xls',

 'https://part.of.url/P2016-17.xls']

I wish to convert it into link form and using the code


    for urlValue in url:
        print(urllib.parse.quote(urlValue))

output I am getting is

https%3A//part.of.url/P2012-13.xls

https%3A//part.of.url/P2013-14.xls

https%3A//part.of.url/P2014-15.xls

https%3A//part.of.url/P2015-16.xls

It should be the list of URL containing list such as below.

https://part.of.url/P2012-13.xls

How can it be solved?

Ruli
  • 2,592
  • 12
  • 30
  • 40
  • 1
    aren't they already valid urls? – Chase Jan 22 '21 at 14:43
  • no they are not valid – FinickyBee Jan 22 '21 at 14:44
  • 1
    can you describe what exactly output you want to get – code by Abhishek Bharti Jan 22 '21 at 14:44
  • 1
    @Sneha explain how `https://part.of.url/P2012-13.xls`, which you claim is an element of your list, is not a valid url. – Chase Jan 22 '21 at 14:46
  • @Abhishek Bharti a valid url (https://part.of.url/P2012-13.xls) will have a colon after https inplace of %3A (https%3A//part.of.url/P2012-13.xls) – FinickyBee Jan 22 '21 at 14:51
  • What Chase is saying is that your original list, *before* processing, already consists of valid URLs. Why are you even trying to change it? – CrazyChucky Jan 22 '21 at 14:53
  • @crazyChucky thanks for explaining. @ chase its in string form which can not be read – FinickyBee Jan 22 '21 at 14:57
  • ...Cannot be read by what? What do you want them to be, if not strings? That's how you store text in Python. – CrazyChucky Jan 22 '21 at 15:02
  • 1
    @Sneha We're not sure about what you want to do with the urls. Do you want to fetch the excel files in the urls? Do you want to validate the urls? Your problem is easier to solve if your intent is clear – Jakob Lindskog Jan 22 '21 at 15:11
  • Based on your [previous question](https://stackoverflow.com/questions/65843258/creating-a-loop-for-urls-in-python/65851622), it looks like you plan to supply these to [`urllib.request.urlretreive`](https://docs.python.org/3/library/urllib.request.html#urllib.request.urlretrieve). If so, strings are fine. – CrazyChucky Jan 22 '21 at 19:49
  • @CrazyChucky Yes – FinickyBee Jan 23 '21 at 13:04
  • @Jakob Lindskog Intent is to have URL (where you click the url and directly reaches to website) and then use urllib.request.urlretreive to downlaod the files in the programme. – FinickyBee Jan 23 '21 at 13:08
  • That's not how it works. For `urllib.request.urlretrieve`, *you want a string*. There's no "clicking" to be done. What you have already is fine. See my [answer](https://stackoverflow.com/a/65851622/12975140) to your previous question for a more thorough explanation. – CrazyChucky Jan 23 '21 at 15:09

2 Answers2

0

I think you don't need to quote. It should work with the following code. If not then please clarify and share your full source.

    for urlValue in url:
        print(urlValue)
Md. Mehedi Hasan
  • 196
  • 2
  • 14
0

"quote" is the wrong method for this. It is used specifically to remove special characters from a string, which is what you are seeing.

If you are trying to build url objects, urllib.parse.urlparse(urlValue) would be the method you want.

As someone new to python- the urllib libraries are pretty low API. Assuming you're trying to make network connections, I would strongly recommend looking at the "requests" library from pypi, which is a much simpler user interface.

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42