0

I have a text file setup like a database.

Host.txt:

4050, xxx.xxx.xxx.xxx, green
4052, xxx.xxx.xxx.xxx, blue
4451, xxx.xxx.xxx.xxx, red

Variable x will contain the string that should match with the first list item of a line in the file.

x = 4052

I need to be able to open this file, and read line by line till it matches the first item in the list with my x variable. When it matches, I want it to put all items in that row from the text file to a list variable such as device[]

printed list would look like:

print device
>>>[4052, xxx.xxx.xxx.xxx, blue]

I have tried this:

device = []
x = '4052'
with open('Host.txt', "r") as f:
  for x in f:
    device.append(x)

print device

With output:

C:\Python27\Scripts>python List.py
['4050, xxx.xxx.xxx.xxx, green\n', '4052, xxx.xxx.xxx.xxx, blue\n', '4451, xxx.x
xx.xxx.xxx, red']

Which is incorrect.

BilliAm
  • 590
  • 2
  • 6
  • 26
  • In what language? Where is the code you've already written? – Evan Trimboli Jul 28 '14 at 01:09
  • What language are you doing this in? And what have you already tried? Post an attempt, explan what's not working, and we can chip in advice/suggestions. – nbrooks Jul 28 '14 at 01:10
  • Python, and I am not sure where to start as no examples I have found come close to what I am asking. – BilliAm Jul 28 '14 at 01:13
  • You can start with [this question](http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array) for reading a file. I assume you know how to do text-value comparisons and how to break out of a loop, otherwise I'd try some basic programming tutorials first. – nbrooks Jul 28 '14 at 01:15
  • I added what I tried even tho I know it's way off base. – BilliAm Jul 28 '14 at 01:25

1 Answers1

0

You're close, but missing some basics. Try the code below.

x = '4052'
with open('Host.txt', 'r') as f:
    for line in f:
        if line.startswith(x):
            device = [word.strip() for word in line.split(',')]
            break

First thing I did was change the variable name in the loop from x to line. This way you wont have a conflict with your search variable x='4052'. Next, you don't want to append every line of the file to your list, you just want the first line that matches your x variable. That is what the startswith() method does.

If you get a match you will have a line with a new line character, '\n', at the end. strip() removes this new line character and any whitespace in each word. split() splits the line at commas so you can "append" each part of the line to a list. Instead of using append though I've used what's called a list comprehension which loops over the list created by split() and adds each item to the new list called device.

Note that the items in device will be strings so if you want to access the first item (for example) as an integer you will have to convert it. Also Note that this will only return the first occurrence in the file. If you want every occurrence, you can create an empty list outside of the for loop, append device to it each time you have a match, and remove the break command.

Gabriel
  • 10,524
  • 1
  • 23
  • 28
  • Excellent: `C:\Python27\Scripts>python List.py ['4052', ' xxx.xxx.xxx.xxx', ' blue']` – BilliAm Jul 28 '14 at 01:54
  • you may also want to use the `strip()` method on the words in the list comprehension to get rid of the white space. I'll add it to the answer. – Gabriel Jul 28 '14 at 01:57
  • Awesome Explanation. This is just to post as strings to a html Page. So leaving them as strings is fine. I hope this answer will benefit others with the same type of data processing needs. Also I just removed the space in the host files inbetween the list items. But I do understand the need to remove the spaces inbetween my list items. I can just nip it in the but via the 'Host.txt' file. – BilliAm Jul 28 '14 at 02:00