Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fin = open("Data.csv", encoding='utf-8') # Open input CSV file as a text file
- fout = open("Data1.csv", 'w') # ** You should edit the filename for each filter
- limit = 0.15 # ** You should edit this variable for the various filters you want to apply
- header = True # The first line of CSV file will be the header
- profit_per_col = 0
- count_in, count_out = 0, 0
- for fline in fin: # Read one line at a time from input CSV file
- count_in += 1 # count number of lines in input file
- csvlist = fline.split(",")
- if header: # If header line ...
- fout.write(fline) # Write the header line to the output file
- count_out += 1 # Count one output line
- header = False # mark that header has been processed
- for col, field in enumerate(csvlist): # get column number and data for each column
- if "profit_per" in field: # look for "profit_per" in header line
- profit_per_col = col # Remember what column is "profit_per"
- break # break out of column loop when we have found the required column
- continue # Go back in outer loop to get next input line
- # From here on we are processing data lines
- profit_per = csvlist[profit_per_col] # Extract the data from the "profit_per" column
- try: # Try converting this data to a float
- pp_float = float(profit_per) # Get profit_per as a float if possible
- if pp_float >= limit: # Check if profit_per passes the filter
- fout.write(fline) # Write filtered data to output file
- count_out += 1 # Count output lines
- except: # "profit_per" column did not contain a float value
- continue # skip this line, go back to read next line
- print(f"{count_in=} {count_out=}") # Print the number of input and output lines in CSV files
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement