I have a txt file to parse that looks like:
--- What kind of submission is this? ---\n Sold Property\n --- State? ---\n Los Angeles\n ...
and need to store values after --- --- tags in variables. It works with all those if statements, but I was wondering whether it is possible to refactor a huge number of ifs into some structure (e. g. dictionary), and then easily write that to output file.
Here's something I made:
"""Open a file to read"""
for line in res:
if "Instagram Usernames" in line:
usernames = next(res)
if "Date" in line:
date = next(res)
if "Address" in line:
address = next(res)
if "Neighborhood" in line:
market = next(res)
if "State" in line:
city = next(res)
if "Asset" in line:
as_type = next(res)
if "Sale Price" in line:
price = next(res)
if "," in price:
price = price.replace(',', '')
if "$" in price:
price = price.replace('$', '')
if "Square" in line:
sf = next(res)
if "," in sf:
sf = sf.replace(',', '')
if "$" in sf:
sf = sf.replace('$', '')
if "Buyer" in line:
buyer = next(res)
if "Seller" in line:
seller = next(res)
if "Broker" in line:
brokers = next(res)
if "Notes" in line:
notes = next(res)
"""Write to output file"""
fin.write("IMAGE: @" + usernames)
fin.write("DATE: " + date)
fin.write("ADDRESS: " + address)
fin.write("MARKET: " + market)
fin.write("CITY: " + city)
if as_type == "Multi Family" or "Multi Family\n":
fin.write("ASSET TYPE: Multifamily\n")
else:
fin.write("ASSET TYPE: " + as_type)
fin.write("PRICE: $" + price)
if sf in bad_symb:
fin.write("SF: N/A\n")
fin.write("PPSF: N/A\n")
else:
fin.write("SF: " + sf)
fin.write("PPSF: $" + "{0:.2f}\n".format(float(price) / float(sf)))
fin.write("BUYER: " + buyer)
fin.write("SELLER: " + seller)
fin.write("BROKERS: " + brokers + "\n")
if notes != "\n":
fin.write("NOTES: " + notes + "\n")
fin.write(footer_sale(market, buyer, seller))
Any help would be appreciated, thanks in advance!
Aucun commentaire:
Enregistrer un commentaire