def extract_info_from_convert_text(path):
    pattern=re.compile(r'^\d{2}\/\d{2}\/\d{4}.*')
    info_list=[]
    with open(path,'rb') as fp:
        for line in fp.readlines():
            match=pattern.search(line.decode('utf-8'))
            if match:
                info_list.append(line.decode('utf-8'))
    return info_list

print(extract_info_from_convert_text('canberra_land_sales_result.txt'))

def extract_date_type_suburb_price_company_from_list(info_list):
    for item in info_list:
        date=item[:10]
        pattern_type=re.compile(r'(?:\d{2}\/\d{2}\/\d{4} +)([a-zA-Z]+)')
        block_type=pattern_type.search(item)
        pattern_price=re.compile(r'\$[0-9,]+')
        price=pattern_price.search(item)
        pattern_suburb=re.compile(r'(?<= {3})[a-zA-Z]+(?= {3})')
        
        info_dict={}
        info_dict['date']=date
        if block_type:
            info_dict['block type']=block_type.groups()[0]
            block_type_position=block_type.end()
            suburb=pattern_suburb.search(item[block_type_position+1:])
            if suburb:
                info_dict['suburb']=suburb.group()
            else:
                info_dict['suburb']=''
        else:
            info_dict['block type']=''
            info_dict['suburb']=''
        if price:
            info_dict['price']=price.group()
            purchaser_offset=price.end()
            purchaser=item[purchaser_offset+1:]
            purchaser=purchaser.replace(' ','')
            info_dict['purchaser']=purchaser
        else:
            info_dict['price']=''
            info_dict['purchaser']=item[-10:].replace(' ','')
                      
        yield info_dict

"""
Explanation: 在fp.readlines()中返回的是个list，每个list中的选项是byte类型的obj,pattern.search的对象必须是str，所以才加上line.decode('utf-8')
End of explanation
"""


plt.style.use('ggplot')
%matplotlib inline
pd.set_option('display.max_columns',100)
pd.set_option('display.max_colwidth',400)
pd.set_option('display.precision',3)
pd.set_option('display.expand_frame_repr', False)

info_list=extract_info_from_convert_text('canberra_land_sales_result.txt')
land_list=list(extract_date_type_suburb_price_company_from_list(info_list))
df=pd.DataFrame(land_list)

print(df)

"""
Explanation: 不加pd.set_option('display.expand_frame_repr', False)这句话，print(df)一行会分裂成两行显示
End of explanation
"""


print(df.to_string())

"""
Explanation: 还是这个函数好,df.to_string()  to force it to show the whole table.
End of explanation
"""


