0

How would parse out a pipe delimited string in Python 3.0? My application is reading various barcodes in a random fashion. The barcodes are as below:

var = '07|415674|88881234564|3326'

I need to evaluate the length of each parsed variable and determine the one barcode I'm looking for.

Thanks for you help! TC

kutschkem
  • 7,826
  • 3
  • 21
  • 56
todd328
  • 51
  • 2
  • 6

2 Answers2

2

You can do something like:

values = '07|415674|88881234564|3326'
findLength = 4

for val in values.split("|"):
    if(len(val) == findLength):
        print("Found: " + val)

This splits the string on | and loops through the resulting array, checking the length of each value and printing it if it matches the findLength variable.

Mark
  • 5,089
  • 2
  • 20
  • 31
1

Using the string.split() method you can give it a delimiter to split on. In this case

var.split('|')

will split var at every pipe and returns a list of strings

samlli
  • 106
  • 5