0

I have a Python script obtained from a project which I am trying to debug however I am unable to resolve one error. Per the author's description of the project, everything works fine.

The script takes a parameter called "ascii" which is of type str as shown below:

parser.add_argument('--ascii', type=str,
                    help='ASCII Data type: ASCII characters')

Per my understanding, in the following code, it processes the input string one character at a time and each character is sent to a function, iter_bin() which will take the ASCII value of the character and convert it to binary, appending the output to a list.

ASCIIDATA = args.ascii
dataArray = []

for line in ASCIIDATA:
            for entry in line:
                # Make sure everything is a number, convert if not
                dataArray.append(''.join(s for s in iter_bin(entry)))


def iter_bin(s):
    sb = s.encode('ascii')
    return (format(b, '07b') for b in sb)

When I run this code, I get the following error:

Traceback (most recent call last):
  File "check.py", line 107, in <module>
    main()
  File "check.py", line 70, in main
    dataArray.append(''.join(s for s in iter_bin(entry)))
  File "check.py", line 70, in <genexpr>
    dataArray.append(''.join(s for s in iter_bin(entry)))
  File "check.py", line 82, in <genexpr>
    return (format(b, '07b') for b in sb)
ValueError: Unknown format code 'b' for object of type 'str'

How can I resolve this error?

Thanks.

Neon Flash
  • 3,113
  • 12
  • 58
  • 96
  • Could you add a sample of your input data? You are passing an int to the function iter_bin and inside the function you call encode that is a string method. – Dani Mesejo Oct 25 '18 at 00:57
  • The input can be any string. As an example: script.py --ascii "1234" – Neon Flash Oct 25 '18 at 00:59
  • I see but the problem is that iter_bin, as far as I understand expects a string and the code is passing an int. I executed iter_bin(2) and it throws an it gives a different error – Dani Mesejo Oct 25 '18 at 01:01
  • Oh, I corrected the code now. It showed the int function there because I was debugging. Could you please check now? Even with the updated code I get the error. – Neon Flash Oct 25 '18 at 01:05
  • 1
    What version of Python are you using? This throws the error in 2.7 but not in 3.5. – Dani Mesejo Oct 25 '18 at 01:08
  • Maybe look at this answer? https://stackoverflow.com/a/18815890/10372572 – Joyal Mathew Oct 25 '18 at 01:15
  • @DanielMesejo: That is correct indeed. It seems to be specific to the version of Python. Also, in another section of the code, for subprocess.call["ping -c 1 " + ip_address], shell=True, stdout=FNULL]), I get an error like: '"ping -c 1 8.8.8.8"' is not recognized as an internal or external command, operable program or batch file." Do you know why I get that error? I am running the Python script on Windows – Neon Flash Oct 25 '18 at 01:23
  • @NeonFlash Sorry but I do not recognize that error – Dani Mesejo Oct 25 '18 at 01:36

0 Answers0