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.