2

I am trying to store strings of data for ocr detection using naive bayes. The data is a string with dimensions 29 X 28. It looks something like this:

        ++#####++       
    ++######++          
    ####+++             
    +##                 
    +##+                
     +#+                
     +#++++             
    +######++           
   +###+++####+         
   +#+     ++###+       
              +##+      
               +##+     
                +##+    
                 +##    
                  +#    
                 +##    
    ++          ++#+    
   +#+      ++++##+     
   +###++#+#####++      
    +++####++++      

To read and store such data into an array of integers I try to .replace '#' and '+' with '1' and '\n' and blank spaces by 0. However, when I try to reconstruct the image after reading into an array. What I get is this:

 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]          
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1]
 [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1]
 [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1]
 [1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
 [1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] 

I don't understand why I am getting a garbled output. The code I am using is:

lines = data.read()


def split28(s, n):
    for begin in range(0, len(s), n):
        yield s[begin:begin+n]


zoenum = []         #strings are immutable
val_zoenum = []


for digit in split28(lines, 812):
    zoenum.append(digit)

for i in range(len(zoenum)):
        zoenum[i] = zoenum[i].replace("\n", " ")        
        zoenum[i] = zoenum[i].replace("+", "1").replace("#", "1").replace(" ", "0")
        zoenum[i] = list(map(int, zoenum[i]))

zoenum is a list of such strings of data. Any thoughts/suggestions are appreciated. Also, if I need to restructure the question, please tell me.

10
  • 1
    are you sure the input does not contain tabs or any non-space whitespace? Commented May 2, 2017 at 18:03
  • 1
    Hard to say. data.read() doesn't give you a list of lines, it gives you a big string, which can be interpreted as an iterator of characters. Commented May 2, 2017 at 18:03
  • Input does contain white spaces(blanks) which I replace by 0s. @Tommy Commented May 2, 2017 at 18:06
  • Yes, that's why I am converting it into a list of integers. @Eric Duminil Commented May 2, 2017 at 18:08
  • 1
    In your example code you don't use lines anywhere. Why do you have it? What does zoenum contain? What is the output of print(repr(zoenum)) before running your loop? And after? Commented May 2, 2017 at 18:12

1 Answer 1

4

You need to make sure that what you call line really is a line. If you have a file object, use readlines() instead of read, or this syntax:

with open('filename.txt') as file:
    for line in file:
        print(line)
        # do something with line

It might be easier to create a new list with a comprehension instead of trying to modify a list in place.

Here's an example with a data included inside the script:

text = """        ++#####++
    ++######++
    ####+++
    +##
    +##+
     +#+
     +#++++
    +######++
   +###+++####+
   +#+     ++###+
              +##+
               +##+
                +##+
                 +##
                  +#
                 +##
    ++          ++#+
   +#+      ++++##+
   +###++#+#####++
    +++####++++         """

for line in text.splitlines():
    print([0 if x == ' ' else 1 for x in line])

It outputs:

[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
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, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

With :

for line in text.splitlines():
    print(''.join('0' if x == ' ' else '1' for x in line))

It outputs:

000000001111111110000000
000011111111110000000000
000011111110000000000000
000011100000000000000000
000011110000000000000000
000001110000000000000000
000001111110000000000000
000011111111100000000000
000111111111111000000000
000111000001111110000000
000000000000001111000000
000000000000000111100000
000000000000000011110000
000000000000000001110000
000000000000000000110000
000000000000000001110000
000011000000000011110000
000111000000111111100000
000111111111111111000000
000011111111111000000000

Note that the dimensions are 24x20, not 29x28.

Sign up to request clarification or add additional context in comments.

1 Comment

You should change x is ' ' to x == ' ' since you mean to test for equality not identity. (It may be a nice implementation detail that Python is interning a single character string, but I think that should probably be saved as a possible later optimization.)

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.