Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading / Parsing IP and Port from file

Posted on 2008-06-22
1
Medium Priority
?
837 Views
Last Modified: 2012-05-05
I'm trying to read and parse the ip and port from a file. I'm pretty close to getting it, but
somethings not right.

The file is data.txt has the contents
IP_ADDRESS=127.0.0.1
PORT=3031

The result after running the program is backwards
PORT:  127.0.0.1

IP:  3031

Why I don't understand and what to do to make PORT: 3031 and IP: 127.0.0.1
myFile = open("data.txt","r")
for line in myFile.readlines():
	if line.find("PORT"):
		ip = line.split("=")[1]
		print "PORT:  " + str(ip)
	elif line.find("IP_ADDRESS"):
		port = line.split("=")[1]
		print "IP:  " + str(port)

Open in new window

0
Comment
Question by:mitchguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
RichieHindle earned 200 total points
ID: 21841838
The "find" method returns -1 if it doesn't find anything, and "if -1" succeeds - that is, -1 is considered to be True.

What you'd better off doing is saying 'if "PORT" in line:" - it's easier to read and it does exactly what you want.

Your "port" and "ip" variables are the wrong way round - you're parsing the port into a variable called "ip" and vice versa.  It's not the cause of any breakage, but it could be the cause of confusion!

An aside: your output is double-spaced because the lines you're iterating through contain the newlines from data.txt, but your "print" statements are adding newlines as well.  Using line.strip() prevents that.

Here's the working code:

myFile = open("data.txt","r")
for line in myFile.readlines():
        line = line.strip()
        if "PORT" in line:
                port = line.split("=")[1]
                print "PORT:  " + str(port)
        elif "IP_ADDRESS" in line:
                ip = line.split("=")[1]
                print "IP:  " + str(ip)

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question