I have a text file I am reading with three regular expressions. I’d like to combine each item from each regex search line by line and print them out using the same format as the last line below. I just cannot get the loop right to combine everything.
Sample Text from three different sources(you can see info is sometimes missing and other times presented in different formats:
-
Bond Name O/F C/F Cpn MTR FICO CAL WALB 1mCPR 60+ CE
CWL 2004-6 2A5 0.95 0.09 L+39 4 49 200 4 28.62 47.69% -
Bond Name O/F C/F Cpn FICO CAL WALB 60+ Notes Offer
CSMC 06-9 7A1 25.00 12.01 L+45 727 26 577 33.29 FLT,AS,0.0% 50-00 -
Type CUSIP Bond Name Term Offer Structure
PRIME 17312KAB8 CMSI 07-5 1A2 7/7 92.50 LCF
import re
string = open("cusip.txt")
read_string = string.read()
cusip_reg_exp = re.compile('\s[0-9]{3}[a-zA-Z0-9]{6}\s')
cusip_result = cusip_reg_exp.findall(read_string)
bond_name_reg_exp = re.compile('\s[A-Z]{3,5}\s[0-9]{4}\D{1,3}\S{1,3}\s{1,2}\w{1,3}')
bond_name_result = bond_name_reg_exp.findall(read_string)
bond_price_name_reg_ex = re.compile('[$]{0,1}[0-9]{1,2}[-]{1}[0-9]{2}')
bond_price_result = bond_price_name_reg_ex.findall(read_string)
print(cusip_result[0],bond_name_result[0],bond_price_result[0])
You can use
zip[docs] oritertools.izip[docs]:Depending on the format of the file, the
csv[docs] module might be helpful too (instead of using regular expressions to extract the content).You could also iterate over each line and extract the relevant information per line.