I need to change the zip code to a series of dots and dashes (barcode), but I cannot figure out how

Here's what I have so far:

def encodeFive(zip):

    zero =  "||:::"

    one =   ":::||"

    two =   "::|:|"

    three = "::||:"

    four =  ":|::|"

    five =  ":|:|:"

    six =   ":||::"

    seven = "|:::|"

    eight = "|::|:"

    nine =  "|:|::"

    codeList = [zero,one,two,three,four,five,six,seven,eight,nine]

    allCodes = zero+one+two+three+four+five+six+seven+eight+nine

    code = ""
    digits = str(zip)
    for i in digits:

        code = code + i    

    return code

      

With this, I will get the original zip code on the string, but none of the numbers are encoded into the barcode. I figured out how to encode one number, but it won't work the same way as five numbers.

+2


a source to share


7 replies


codeList = ["||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" ]
barcode = "".join(codeList[int(digit)] for digit in str(zipcode))

      



+4


a source


Maybe use a dictionary:

barcode = {'0':"||:::",
           '1':":::||",
           '2':"::|:|",
           '3':"::||:",
           '4':":|::|",
           '5':":|:|:",
           '6':":||::",
           '7':"|:::|",
           '8':"|::|:",
           '9':"|:|::",
           }

def encodeFive(zipcode):
    return ''.join(barcode[n] for n in str(zipcode))

print(encodeFive(72353))
# |:::|::|:|::||::|:|:::||:

      



PS. It is best not to name the variable zip

, as this overrides the built-in function zip

.
Likewise, it is best not to name the variable code

as it code

is a module
in the standard library.

+2


a source


You just add i

(character in digits

) to the line where I think you want to add codeList[int(i)]

.

The code will probably be much simpler, just using a dict to search.

+1


a source


I find it easier to use split () to create lists of strings:

codes = "||::: :::|| ::|:| ::||: :|::| :|:|: :||:: |:::| |::|: |:|::".split()

def zipencode(numstr): 
    return ''.join(codes[int(x)] for x in str(numstr))

print zipencode("32345") 

      

+1


a source


I don't know what language you are using m, so I made an example in C #:

int zip = 72353;

string[] codeList = {
  "||:::", ":::||", "::|:|", "::||:", ":|::|",
  ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"
};
string code = String.Empty;
while (zip > 0) {
  code = codeList[zip % 10] + code;
  zip /= 10;
}
return code;

      

Note. Instead of converting the zip code to a string and converting each character back to a number, I calculated the digits numerically.

Just for fun, here's a one-liner:

return String.Concat(zip.ToString().Select(c => "||::::::||::|:|::||::|::|:|:|::||::|:::||::|:|:|::".Substring(((c-'0') % 10) * 5, 5)).ToArray());

      

0


a source


This is done in python.

number = ["||:::",
    ":::||",
    "::|:|",
    "::||:",
    ":|::|",
    ":|:|:",
    ":||::",
    "|:::|",
    "|::|:",
    "|:|::"
    ]
def encode(num):
    return ''.join(map(lambda x: number[int(x)], str(num)))

print encode(32345)

      

0


a source


You seem to be trying to generate a "postnet" barcode. Please note that five-digit Postnet PostScript barcodes were deprecated by ZIP + 4 postnet barcodes, which were deprecated by Postnet ZIP + 4 + 2 barcodes, all of which must include a checksum digit, as well as upper and trailing frames ... In any case, all these forms are obsolete with the new "smart postal" 4-axis barcodes, which require a lot of computational code to generate and no longer rely on direct digit comparisons in bars. See USPS.COM for details.

0


a source







All Articles