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.
a source to share
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.
a source to share
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());
a source to share
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.
a source to share