Home > Blockchain >  Iterating through a python array does not work
Iterating through a python array does not work

Time:04-15

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

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?

This question has been answered. Here's the end-product: https://pastebin.com/TaVKDVfU

CodePudding user response:

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])

CodePudding user response:

The print function in python has an 'end' parameter that defaults to newline \n.

Since each character is read in individually, i is a single character followed by \n

Try print(i, end = '')

CodePudding user response:

f.read() return the content of your text file in a string, you cannot convert it to a list of tuples. Well, you could but using a lot of split() (I recomend to split on ")," or "(" to separate each tuple, then on "," to get each element of the tuple). Something like:

with open("example.txt", "r") as f:
    your_list: list[tuples]

    # read the file as a string
    f_in_str: str = f.read()

    # removing useless characters
    f_in_str = f_in_str.replace("[", "")
    f_in_str = f_in_str.replace("]", "")
    f_in_str = f_in_str.replace("(", "")
    f_in_str = f_in_str.replace(" ", "")
    f_in_str = f_in_str.replace("'", "")

    # split it into a list
    tuples_in_str: list[str] = f_in_str.split("),")

    # convert str to tuples
    for tuple_str in tuples_in_str:
        # split() returns a list that we convert into a tuple
        a_tuple: tuple = tuple(tuple_str.split(","))
        your_list.append(a_tuple)

Note that i have not tested that code.

I strongly advise you, if you can, to change the format of your source to something like a csv. Il will make things a lot easier in the futur.

  • Related