0

I have just started with Python and I am having trouble parsing strings. I am very much a newbie and I am just playing around with the basics for now. I thought I'd start with parsing trivial stuff like the output of pi_info() just to get some practice with text parsing.

Here is my script:

import gpiozero

boardinfo_length = None
counter = 1

boardinfo = gpiozero.pi_info()
for value in boardinfo:
 if counter != 15:
 print('[', end=' ')
 print(counter, end=' ')
 print(']', end=' ')
 print(boardinfo_length, end=' ')
 print(value)
 print()
else:
 print('[', end=' ')
 print(counter, end=' ')
 print(']', end=' ')
 split_string = value.split(":")
 for split_string_value in split_string:
 print(split_string_value)
 counter += 1

Here is the output:

[ 1 ] None a02082

[ 2 ] None 3B

[ 3 ] None 1.2

[ 4 ] None 2016Q1

[ 5 ] None BCM2837

[ 6 ] None Sony

[ 7 ] None 1024

[ 8 ] None MicroSD

[ 9 ] None 4

[ 10 ] None 1

[ 11 ] None True

[ 12 ] None True

[ 13 ] None 1

[ 14 ] None 1

[ 15 ] Traceback (most recent call last):
File "boardinfo.py", line 19, in <module>
split_string = value.split(":")
AttributeError: 'dict' object has no attribute 'split'

Can someone help me figure it out ...

2
  • 2
    Looks like your "value" variable in your loop is a dict object not a string. You should make another loop for your "value" variable since its a dictionary and print that, thats probably what you're looking for. Commented Aug 13, 2019 at 17:50
  • What are you trying to figure out? If you're trying to parse a particular string format for a particular use, then put the problem string directly into your posting, and eliminate the superfluous code. The error message is quite clear: value is a dict at that point; you tried to apply a string method. If needed, insert useful print statements to display the data you're getting. See this lovely debug blog for help. Commented Aug 13, 2019 at 18:02

1 Answer 1

1

The type of the last value is a dictionary, not a string. Since dicts don't have the split()-method, i probably would check the type of value first:

for value in boardinfo:
    if isinstance(value, str):
        # String handling
    elif isinstance(value, dict):
        print("This is a dictionary: " + str(value))
    else:
        print("value has type: " + str(type(value)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.