Search code examples
pythonfor-loopraspberry-pii2c

How to save an i2c address in python


hi i found this code in internet to detect i2c address in python on raspberry pi 3, it works well the result im getting is: 10: 10 11 but i want to save each result in a different variable for example x=10 y=11, the first 10 doesnt matters, any idea? im aprreciate your answer!

#!/usr/bin/python3

import os
import subprocess
import re

p = subprocess.Popen(['i2cdetect', '-y','1'],stdout=subprocess.PIPE,)

for i in range(0,9):
  line = str(p.stdout.readline())

  for match in re.finditer("[0-9][0-9]:.*[0-9][0-9]", line):
    print (match.group())

Solution

  • It seems you don't know how to split text into words and convert to numbers

    text = "10: 10 11"
    
    words = text.split(" ") # ["10:", "10", "11"]
    x = int(words[1])
    y = int(words[2])
    
    print(x, y)
    
    # 10 11