what are you trying to print out? Each tuple or each IP? Just an FYI it's not an array in Python it is a list.
I have just done this.
data = [('192.168.0.59', 2881, '192.168.0.199', 0, 6), ('192.168.0.199', 0, '192.168.0.59', 0, 1), ('192.168.0.59', 2882, '192.168.0.199', 0, 6)]
for item in data:
print(data)
And got the following:
('192.168.0.59', 2979, '192.168.0.199', 0, 6)
('192.168.0.59', 2980, '192.168.0.199', 0, 6)
('192.168.0.59', 2981, '192.168.0.199', 0, 6)
('192.168.0.59', 2982, '192.168.0.199', 0, 6)
('192.168.0.59', 2983, '192.168.0.199', 0, 6)
But I have done the same as you and got the same:
with open("data.txt", "r") as f:
data = f.read()
for item in data:
print(item)
But if you were to do something like print(type(data)) it would tell you it's a string. So that's why you're getting what you're getting what you're getting. Because you're iterating over each item in that string.
with open("data.txt", "r") as f:
data = f.read()
new_list = data.strip("][").split(", ")
print(type(data)) # str
print(type(new_list)) # list
Therefore you could split() the string which will get you back to your list. Like the above...having said that I have tested the split option and I don't think it would give you the desired result. It works better when using ast like so:
import ast
with open("data.txt", "r") as f:
data = f.read()
new_list = ast.literal_eval(data)
for item in new_list:
print(item)
This prints out something like:
('192.168.0.59', 6069, '192.168.0.199', 0, 6)
('192.168.0.59', 6070, '192.168.0.199', 0, 6)
('192.168.0.59', 6071, '192.168.0.199', 0, 6)
Update
Getting the first IP
import ast
with open("data.txt", "r") as f:
data = f.read()
new_list = ast.literal_eval(data)
for item in new_list:
print(item[0])