so I would like to from a input.txt file, create a dictionary
for example, here is sample of the input.txt file
%. VAR %first=Billy
%. VAR %last=Bob
%. PRINT VARS
%. VAR %petName=Gato
%. VAR %street="1234 Home Street"
%. VAR %city="New York"
%. VAR %state=NY
%. VAR %zip=21236
%. VAR %title=Dr.
%. PRINT VARS
%. FORMAT LM=5 JUST=LEFT
%. PRINT FORMAT
so VAR %varName=value
i.e in the case of %first=Billy you would get something like varDict = {"first": "Billy"} right?
Now I wanna know how to do that thru the entire file
There are two dictionaries that I would need to populate, one for the variables, and one for FORMAT, which just holds values, doesn't actually do anything for now.
As far as a desired output, I'm thinking of something of this manner, I would use the pprint function like this pprint.pprint(varDict , width=30) and would output something like this
{'first': 'Billy',
'last': 'Bob'}
{'city': 'New York',
'first': 'Billy',
'last': 'Bob',
'petName': 'Gato',
'state': 'NY',
'street': '1234 Home Street',
'title': 'Dr.',
'zip': '21236'}
{'BULLET': 'o',
'FLOW': 'YES',
'JUST': 'LEFT',
'LM': '5',
'RM': '80'}
EDIT
I am going to input the code I have now for my setFormatWIP.py
import re
import sys
import pprint
input=(sys.argv[1])
regexFormat = re.compile(r'^%\.\s*?FORMAT\s*?((?:(?:\w+)=(?:\w+)\s*)*)$', re.MULTILINE)
regexPrintFORMAT = re.compile(r'^%\.\s*PRINT\s(FORMAT)',re.MULTILINE)
file = open(input)
line = file.readline()
formatDict = dict()
while line:
formatList = regexFormat.findall(line)
printFormatObj = regexPrintFORMAT.search(line)
if printFormatObj != None:
pprint.pprint(formatDict, width=30)
for param in formatList[0].split():
splitParam = param.split('=')
formatDict[splitParam[0]] = splitParam[1]
line = file.readline()
file.close()
running that, i get this error
Traceback (most recent call last):
File "formatTest.py", line 19, in <module>
for param in formatList[0].split():
IndexError: list index out of range
'first': 'Billy', 'last': 'Bob'in the first and second dictionary, but from the example file it looks like it should only be in the first one, also in your regex you are looking for@but in the file each line starts with%. Are these things on purpose? Moreover, some of the values are surrounded by quotes (e.g."New York") and others aren't (e.g.Gato)'first': 'billy'etc, that is actually from thevarDictie, from the first dictionary. There would be too, you see after there is bullet, flow, etc? well that would be from theformatDicti would create. And Yes, for those values surrounded by quotes, what should I do to get that info in that way with the space