i want create text file non subscriptable objects in python , not know how proceed. want extract each month of list , create separate row each. here how list of non subscriptable objects , error like:
the code use following:
months=['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'novemeber', 'december'] in range(len(sections)): if (' tax ' in sections[i] or ' tax ' in sections[i] or ' policy ' in sections[i] or ' policy ' in sections[i]): pat=re.compile("|".join([r"\b{}\b".format(m) m in months]), re.m) month = pat.search("\n".join(sections[i].splitlines()[0:6])) print(month) outfile = open('h:/uncertainty_data/a_2005_months.txt', 'w') outfile.writelines(month['match']) outfile.close
any appreciate it!
cheers,
month
regex match object, , if want access text matched it, can't index (it's not iterable).
you want use
outfile.write(month.group(0))
although (since you're doing outside of loop) write last month matched. if want collect matches , write them file, like
outputs = [] pat = re.compile("|".join([r"\b{}\b".format(m) m in months]), re.m) section in sections: if any(item in section item in (' tax ', ' tax ', ' policy ', ' policy ')): month = pat.search("\n".join(section.splitlines()[0:6])) outputs.append(month.group(0)) open('h:/uncertainty_data/a_2005_months.txt', 'w') outfile: outfile.writelines(outputs)
Comments
Post a Comment