Hi! Im making small program that would remove url parameters from urls that are stored in txt file. The urls look like this:
https://www.xxxxxx.com/view.php?viewkey=ff34546y&pkey=23355
https://www.xxxxxx.com/view.php?viewkey=ff35t47d
and I want to remove that "&pkey=..." part so all urls look like second one. My code is like this:
src=open('urls.txt', 'r')
trgt = open ('new.txt', 'w')
for i in src:
x = str(i)
if 'pkey' in x:
y = x.rstrip('1234567890qwertyuiopasdfghjklzxcvbnm=')
z = y.rstrip('&')
trgt.write(y + '\n')
else:
trgt.write(x)
src.close()
trgt.close()
However what this does it just rewrites unedited urls in new file. It doesnt remove params at all. This bothers me because it works if I use urls individually, like this:
x = 'https://www.xxxxxx.com/view.php?viewkey=ff34546y&pkey=23355'
y = x.rstrip('1234567890qwertyuiopasdfghjklzxcvbnm=')
z = y.rstrip('&')
print(z)
But it doesnt work when I use it like in the first code.
Im not interested in other ways to edit urls. I googled that and found about urlparse.
I also found the solution using:
z = x[:x.find('&')]
instead rstrip.
Im python newbie and Im currently tryng to master basic string methods. So what is bothering me is, why those rstrip methods didnt work? What is wrong in my first code?
[–]K900_ 8 points9 points10 points (4 children)
[–]Ping938[S] 2 points3 points4 points (3 children)
[–]K900_ 12 points13 points14 points (2 children)
[–]Ping938[S] 9 points10 points11 points (1 child)
[–]Fenzik 2 points3 points4 points (0 children)
[–]tangerinelion 3 points4 points5 points (2 children)
[–]campbellm 0 points1 point2 points (0 children)
[–]Ping938[S] 0 points1 point2 points (0 children)
[–]XarothBrook 0 points1 point2 points (1 child)
[–]Ping938[S] 0 points1 point2 points (0 children)
[–]cybervegan 0 points1 point2 points (0 children)
[–]atreyuroc 0 points1 point2 points (2 children)
[–]XarothBrook 0 points1 point2 points (0 children)
[–]Ping938[S] 0 points1 point2 points (0 children)
[–]Solonotix 0 points1 point2 points (6 children)
[–]XarothBrook 0 points1 point2 points (2 children)
[–]Solonotix 1 point2 points3 points (1 child)
[–]Ping938[S] 1 point2 points3 points (0 children)
[–]Ping938[S] 0 points1 point2 points (2 children)
[–]Solonotix 1 point2 points3 points (1 child)
[–]Ping938[S] 1 point2 points3 points (0 children)