1

I have JSON in an array that i am importing into my script

"ip_address": [
"192.168.0.1",
"192.168.0.2",
"192.168.0.3"
]

I am loading the JSON and declaring a variable titled ip_address.

data = yaml.load(message)
    for d in data: 
        ip_address = (d.get('ip_address'))

I am attempting to loop using python through the ip addresses.

for address in data['ip_address']:  
      print(address)

I am now getting an error that tells me

string indices must be integers, not str
3
  • 4
    show how ip_address variable is created Commented May 31, 2017 at 12:20
  • 7
    Paste full code. This part is useless. Commented May 31, 2017 at 12:21
  • 4
    Please do not vandalize your posts. Once you've posted a question, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see What is the proper route for a disassociation request? Commented May 31, 2017 at 13:39

3 Answers 3

4

The reason why it's printing individual numbers is because the address is a string. Hence, it's not really each number that's being printed, but rather each letter of a string. Consider:

word = "abc"
for letter in word:
    print(letter)

# prints:
# a
# b
# c

Therefore, it means somewhere you're assigning individual IP addresses to a variable, and then iterate through that variable (which is a string). Without you providing more code on how you get the ip_address variable, it's hard to say where the problem is.

One way to print your IP addresses (assuming you have them in a dict):

addresses = {"ip_address": [
    "192.168.0.1",
    "192.168.0.2",
    "192.168.0.3"
]}

for address in addresses["ip_address"]:  # this gets you a list of IPs
      print(address)

Even if you have them somewhere else, the key insight to take away is to not iterate over strings, as you'll get characters (unless that's what you want).

Updated to address edits

Since I do not have the file (is it a file?) you are loading, I will assume I have exact string you posted. This is how you print each individual address with the data you have provided. Note that your situation might be slightly different, because, well, I do not know the full code.

# the string inside load() emulates your message
data = yaml.load('"ip_address": ["192.168.0.1", "192.168.0.2", "192.168.0.3"]')
ip_addresses = data.get('ip_address')

for address in ip_addresses: 
    print(address)
Sign up to request clarification or add additional context in comments.

3 Comments

Ran that, but I'm getting an error :list indices must be integers, not str
That was only an example - it will most likely not work in the code you have (as I do not know how it looks). You should edit your question to include more information. For starters, where ip_address in for loop is defined.
I have updated the answer. I'm still a little unclear what the message in yaml.load(message) looks like exactly, so it's a bit hard to guarantee this will work. However, the idea of what must be done should be clear. If it doesn't, please update the question specifying what message looks like (a small, working snippet is enough) and I will try to help.
2

In your case ip_address = '192.168.0.1'

Are you sure you have the right value in ip_address?

3 Comments

Yes, this error only occurs when i put the for loop in. This is a loop problem, cant figure out why its looping through individual characters and not strings.
because you set ip_address to a string not to a list. you can print the type of the variable: print(type(ip_address))
for ip in obj['ip_address'] will work fine where obj is the loaded json
1

I tried following :

ip_address = [ "192.168.0.1", "192.168.0.2", "192.168.0.3" ]

>>> ip_address = [ "192.168.0.1", "192.168.0.2", "192.168.0.3" ]

>>> 

>>> for i in ip_address:  

...     print i

... It printed

192.168.0.1

192.168.0.2

192.168.0.3

and it seems to be working fine for me .

1 Comment

because you're iterating through a list and that wasn't the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.