Re: iterating over a list and printing

From: Peter Otten (__peter___at_web.de)
Date: 02/11/04


Date: Wed, 11 Feb 2004 16:38:01 +0100

Bart Nessux wrote:

> ip_list = []
> inputFile = file('ips.txt', 'r')
> ip_list.append(inputFile.read())

You are reading the entire file in one big string and append it to the list
which will always have one big item.

> inputFile.close()
> for i in ip_list:
> print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast
> 128.173.123.255 up" %i
>
> The last line does not work. It prints the first part (/sbin/ifconfig),
> then the entire list of ips, then the second part (netmask 255.255.252.0
> broadcast 128.173.123.255 up). Any ideas on how to fix this? The desired
> results are to print a line for each IP.
>
> /sbin/ifconfig IP1 netmask 255.255.252.0 broadcast 128.173.123.255 up
> /sbin/ifconfig IP2 netmask 255.255.252.0 broadcast 128.173.123.255 up
> etc...

Assuming the file contains IPs one at a line, either initialize

ip_list = inputFile.readlines()

or entirely omit the intermediate list:
(untested)

for line in file("ips.txt"):
    print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast 128.173.123.255
up" % line.strip()

strip() removes any leading/trailing whitespace including the newline
character at the end.

Peter



Relevant Pages

  • Re: Learning the fretboard..
    ... Sight reading at a higher level entails recognition of musical ... is your default handling of two whole steps along a string, ... -Start with a major triad, in root position, with the root on the ... There is only one way to play this triad. ...
    (rec.music.classical.guitar)
  • Re: Parsing / " characters
    ... have the rest of the code to append the ... - JCL inserts that string at the start of every program, ... >>the code associated with that program - in exact order, ... >>Dim db As Database, tdf As TableDef ...
    (microsoft.public.access.externaldata)
  • Re: Parsing / " characters
    ... create a new record in table TTT containing the string ... >I can append ALL the records from the ... >Dim db As Database, tdf As TableDef ... >> Dim lngFN as Long ...
    (microsoft.public.access.externaldata)
  • Re: Fastest way to append text to a TextBox
    ... What's the fastest way to append text to a TextBox? ... CPU and, even if I limit the TextBox to just 30KB of text, it takes over ... Private Sub AppendText(ByVal sText As String) ... Dim iLen As Integer = .Text.Length + sText.Length ...
    (microsoft.public.dotnet.languages.vb)
  • RE: SaveWorkbookAsToday()
    ... Create a string variables for the day and convert your date to the format you ... Then append or concatenate the string date where necessary in the file name. ... Dim DateFormat As String ...
    (microsoft.public.excel.programming)