Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterating through a python array does not work

The following code spits out following:

...
6
)
,
 
(
'
1
9
2
.
1
6
8
.
0
.
5
9
'
,
 
6
0
7
1
,
 
'
1
9
2
.
1
6
8
.
0
.
1
9
9
'
,
 
0
,
 
6
)
]

The code:

f=open("example.txt", "r")
list=f.read()

for i in list:
    print(i)

example.txt: https://pastebin.com/uN5wVZKL

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The example.txt looks like following: [(IP,number,IP,number,number),(IP,number,IP,number,number),(IP,number,IP,number,number),...]

I thought I could iterate through that array to get the first IP in every tuple in the array but it just iterates character through character instead of element through element.

What am I doing wrong?

>Solution :

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 sting 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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading