# Extracting data from QR to a form

**URL:** <https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628>\
**Category:** Ignition\
**Created:** [August 21, 2019, 12:34pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628 "2019-08-21T12:34:18Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![warren.johnson](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@warren.johnson](https://forum.inductiveautomation.com/u/warren.johnson)\
**Post date:** [August 22, 2019, 12:42pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/21 "2019-08-22T12:42:04Z")

</div>

Yeah, probably so. Thank you for all the help though

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 22, 2019, 12:47pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/22 "2019-08-22T12:47:17Z")

</div>

The regexes are looking for separators that don’t seem to exist in your QR.

Try using the string method.

```python
result = event.toDict()
fieldDelimiterList = ['(GS1-241)','(GS1-10)','(GS1-37)']

parsedData = parseString(result['Default'][0] , fieldDelimiterList)

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 22, 2019, 12:50pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/23 "2019-08-22T12:50:43Z")

</div>

Your original code seemed to be the what you needed, here is another example I found from sepasoft that you probably already had, but this is probably the best

```auto
Sample #1 onBarcodeReceived event script:
  
#Sample Use of the Python Dictionary method to get barcode values from a single-pass #decode method
results = event.toDict() # converts Java hashtable to Python dictionary
#Sample method to print out all result values
for key, value in results.items():
    print(key, value)
  
#Look for an error message and display it to text box
if event.hasErrorMessage():
 event.source.parent.getComponent('txtError').text = event.getErrorMessage()        
 system.util.beep()        
else:
 #Update screen components from decode results
 if 'GS1-241' in results:
        event.source.parent.getComponent('txtPartNumber').text = results['GS1-241'][0]
 if 'GS1-10' in results:
         event.source.parent.getComponent('txtLotNumber').text = results['GS1-10'][0]
 if 'GS1-37' in results:
         event.source.parent.getComponent('numQty').int = results['GS1-37'][0]

```

---

<div class="post-metadata">

**Author:** ![warren.johnson](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@warren.johnson](https://forum.inductiveautomation.com/u/warren.johnson)\
**Post date:** [August 22, 2019, 2:58pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/25 "2019-08-22T14:58:13Z")

</div>

> [@dkhayes117](#):
>
> Your original code seemed to be the what you needed, here is another example I found from sepasoft that you probably already had, but this is probably the best
> 
> ```python
> Sample #1 onBarcodeReceived event script:
>   
> #Sample Use of the Python Dictionary method to get barcode values from a single-pass #decode method
> results = event.toDict() # converts Java hashtable to Python dictionary
> #Sample method to print out all result values
> for key, value in results.items():
> print(key, value)
>   
> #Look for an error message and display it to text box
> if event.hasErrorMessage():
> event.source.parent.getComponent('txtError').text = event.getErrorMessage()        
> system.util.beep()        
> else:
> #Update screen components from decode results
> if 'GS1-241' in results:
> event.source.parent.getComponent('txtPartNumber').text = results['GS1-241'][0]
> if 'GS1-10' in results:
> event.source.parent.getComponent('txtLotNumber').text = results['GS1-10'][0]
> if 'GS1-37' in results:
> event.source.parent.getComponent('numQty').int = results['GS1-37'][0]
> 
> ```

I tried this and now instead of an error popping up, I get an error in the textError box that says 'No matching patterns were found in the raw barcode'

---

<div class="post-metadata">

**Author:** ![warren.johnson](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@warren.johnson](https://forum.inductiveautomation.com/u/warren.johnson)\
**Post date:** [August 22, 2019, 2:59pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/26 "2019-08-22T14:59:11Z")

</div>

> [@JordanCClark](#):
>
> The regexes are looking for separators that don't seem to exist in your QR.
> 
> Try using the string method.
> 
> ```python
> result = event.toDict()
> fieldDelimiterList = ['(GS1-241)','(GS1-10)','(GS1-37)']
> 
> parsedData = parseString(result['Default'][0] , fieldDelimiterList)
> 
> ```

With this I get a KeyError: Default

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 22, 2019, 3:04pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/27 "2019-08-22T15:04:16Z")

</div>

Is Default enabled in the Pattern configuration?

---

<div class="post-metadata">

**Author:** ![warren.johnson](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@warren.johnson](https://forum.inductiveautomation.com/u/warren.johnson)\
**Post date:** [August 22, 2019, 3:07pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/28 "2019-08-22T15:07:56Z")

</div>

It wasn’t selected anymore but I went back and selected and no still get the same error.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 22, 2019, 3:28pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/29 "2019-08-22T15:28:48Z")

</div>

Did you save the project after the change? 😉

This is the script I am using:

```python
def parseString(stringIn, delimiters):
	########################################################
	## ##
	## parse a delimited string and return a dictionary ##
	## ##
	## NOTE: Be sure the delimiter list is in the same ##
	## order as they appear in the string. Save ##
	## yourself some pain. :) ##
	## ##
	########################################################
	
	
	# Create a list of lists that contains the indexes of where each delimiter appears,
	# and the length of the delimiter, so we can use them to say where each field data lies.
	indexList = [[stringIn.index(delimiter), len(delimiter)] for delimiter in delimiters]

	# This is just a pointer that we'll use later to flag when the last iteration of the 
	# following loop is in play.
	last = len(indexList) - 1

	# Initialize the output dictionary.
	dictOut = {}

	# populate the output dictionary with the field data
	for index, delimiter in zip(range(len(indexList)), delimiters):
		# set the start of each field data
		start = indexList[index][0] + indexList[index][1]
		# set the end of the field data. This is treated differently if it 
		# is the last iteration.
		if index != last:
			end = indexList[index + 1][0]
		else:
			end = len(stringIn)
		# Add to the output dictionary
		dictOut[delimiter] = stringIn[start:end]
		
	return dictOut
	
result = event.toDict()

print '--- result ---'
print result
print '--------------'
print

fieldDelimiterList = ['(GS1-241)','(GS1-10)','(GS1-37)']

parsedData = parseString(result['Default'][0], fieldDelimiterList)

print '--- parsedData ---'
print parsedData
print '--------------'

```

This is what I have in my console:

 ![2019-08-22_11-26-42](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c8e777e400f27305da7cc749cf5c7820fc38e8ac.jpeg)

---

<div class="post-metadata">

**Author:** ![warren.johnson](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@warren.johnson](https://forum.inductiveautomation.com/u/warren.johnson)\
**Post date:** [August 22, 2019, 4:33pm UTC](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628/30 "2019-08-22T16:33:40Z")

</div>

> [@JordanCClark](#):
>
> The regexes are looking for separators that don't seem to exist in your QR.
> 
> Try using the string method.
> 
> ```python
> result = event.toDict()
> fieldDelimiterList = ['(GS1-241)','(GS1-10)','(GS1-37)']
> 
> parsedData = parseString(result['Default'][0] , fieldDelimiterList)
> 
> ```

So apparently there was an issue with the scanner I was using. Once I got a new one and set the prefix, this worked.

I want to thank you both for all your help. Between code and bad hardware, it made it tougher than I thought it would be.

[Previous page](https://forum.inductiveautomation.com/t/extracting-data-from-qr-to-a-form/27628.md?page=1)
