Skip to content

Instantly share code, notes, and snippets.

@toinetoine
Last active September 11, 2015 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toinetoine/1708051e20578c0168e4 to your computer and use it in GitHub Desktop.
Save toinetoine/1708051e20578c0168e4 to your computer and use it in GitHub Desktop.
Reading items file (stackechange awsner for http://stats.stackexchange.com/questions/172100)
# parse the intput file in format:
# more than one space between columns
def parse_item_file(items_info_filename):
items_file = open(items_info_filename, "r")
items_info = list()
column_names = list()
first_parsed = False
for item_row in items_file:
# grab the items in the row place in item_components list
raw_item_components = item_row.split(" ")
item_components = list()
for raw_component in raw_item_components:
if len(raw_component.strip()) > 0:
item_components.append(raw_component.strip())
# if reading first row -> set as the column headers
if not first_parsed:
column_names = item_components[:]
first_parsed = True
continue
# for all other rows add them to the items_info list
else:
this_row_info = {}
component_i = 0
for attribute in column_names:
this_row_info[attribute] = item_components[component_i]
component_i += 1
items_info.append(this_row_info)
items_file.close()
return items_info
# custom formula you use for getting the value of the item using the
# attributes: Search_Vol, Competition, and Bid.
# right now just random custom formula of:
# value = (search_vol/(competion*1000)) * bid
def get_item_val(search_vol, competition, bid):
item_val = (search_vol / (competition * 1000.0)) * bid
return item_val
# rank items in descending order according to formaula defined in get_item_val
def rank_items(items_info):
item_rankings = list()
# grab the items from the file and get their item value
for item in items_info:
if "Search_Vol" in item and "Competition" in item and "Bid" in item:
search_vol = item["Search_Vol"].replace(" ", "").replace(",", "")
competition = item["Competition"].replace(" ", "").replace("%", "")
bid = item["Bid"].replace(" ", "").replace("$", "")
item_val = get_item_val(float(search_vol), float(competition), float(bid))
item["val"] = item_val
item_rankings.append(item)
# sort the items in descending order by their value
from operator import itemgetter
item_rankings = sorted(item_rankings, key=itemgetter('val'), reverse=True)
return item_rankings
# write the ranked items to the file in csv format
def write_to_result_file(result_file_name, ranked_items):
# write sorted items to the result file
result_file = open(result_file_name, "w")
written_first = False
column_headers = list()
for item in ranked_items:
# for first row, store the column headers
if not written_first:
for key in item.keys():
if key != "val":
result_file.write(key + ",")
column_headers.append(key)
written_first = True
result_file.write("\n")
# write each column to the file
for key in column_headers:
if key in item:
result_file.write(str(item[key]).replace(",", ""))
result_file.write(",")
result_file.write("\n")
result_file.close()
# do it
items_info = parse_item_file("items.txt")
ranked_items = rank_items(items_info)
write_to_result_file("sorted_items.csv", ranked_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment