using variable in a url in python

2024/9/21 1:30:43

Sorry for this very basic question. I am new to Python and trying to write a script which can print the URL links. The IP addresses are stored in a file named list.txt. How should I use the variable in the link? Could you please help?

# cat list.txt192.168.0.1
192.168.0.2
192.168.0.9

script:

import sys
import osfile = open('/home/list.txt', 'r')for line in file.readlines():source = line.strip('\n')print sourcelink = "https://(source)/result”
print link

output:

192.168.0.1
192.168.0.2
192.168.0.9
https://(source)/result

Expected output:

192.168.0.1
192.168.0.2
192.168.0.9
https://192.168.0.1/result
https://192.168.0.2/result
https://192.168.0.9/result
Answer

You need to pass the actual variable, you can iterate over the file object so you don't need to use readlines and use with to open your files as it will close them automatically. You also need the print inside the loop if you want to see each line and str.rstrip() will remove any newlines from the end of each line:

with open('/home/list.txt') as f:  for ip in f:print "https://{0}/result".format(ip.rstrip())

If you want to store all the links use a list comprehension:

with  open('/home/list.txt' as f:links = ["https://{0}/result".format(ip.rstrip()) for line in f]

For python 2.6 you have to pass the numeric index of a positional argument, i.e {0} using str.format .

You can also use names to pass to str.format:

with open('/home/list.txt') as f:for ip in f:print "https://{ip}/result".format(ip=ip.rstrip())
https://en.xdnf.cn/q/72284.html

Related Q&A

Create dynamic updated graph with Python

I need to write a script in Python that will take dynamically changed data, the source of data is not matter here, and display graph on the screen. I know how to use matplotlib, but the problem with m…

Converting a nested dictionary to a list

I know there are many dict to list questions on here but I cant quite find the information I need for my situation so Im asking a new quetion.Some background: Im using a hierarchical package for my mod…

Pandas dataframe : Operation per batch of rows

I have a pandas DataFrame df for which I want to compute some statistics per batch of rows. For example, lets say that I have a batch_size = 200000. For each batch of batch_size rows I would like to ha…

Combining grid/pack Tkinter

I know there have been many questions on grid and pack in the past but I just dont understand how to combine the two as Im having difficulties expanding my table in both directions (row/column).Buttons…

Mocking assert_called_with in Python

Im having some trouble understanding why the following code does not pass:test.pyimport mock import unittestfrom foo import Fooclass TestFoo(unittest.TestCase):@mock.patch(foo.Bar)def test_foo_add(self…

Python select() behavior is strange

Im having some trouble understanding the behavior of select.select. Please consider the following Python program:def str_to_hex(s):def dig(n):if n > 9:return chr(65-10+n)else:return chr(48+n)r = wh…

Moving items up and down in a QListWidget?

In a QListWidget I have a set of entries. Now I want to allow the user to sort (reorder) these entries through two buttons (Up/Down).Heres part of my code:def __init__(self):QtGui.QMainWindow.__init__(…

How to resize a tiff image with multiple channels?

I have a tiff image of size 21 X 513 X 513 where (513, 513) is the height and width of the image containing 21 channels. How can I resize this image to 21 X 500 X 375?I am trying to use PILLOW to do …

Losing elements in python code while creating a dictionary from a list?

I have some headache with this python code.print "length:", len(pub) # length: 420pub_dict = dict((p.key, p) for p in pub)print "dict:", len(pub_dict) # length: 163If I understand t…

How to set Font size or Label size to fit all device

How to set kivy font size or label size so that it will fit all phone-device screen? (fit by means does not overlap with the boundary, rectangle, or even the screen)I know the Buttons and some other W…