Advertisement
acclivity

pyCSV-Read-and-Filter

Nov 17th, 2022
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1.  
  2. fin = open("Data.csv", encoding='utf-8')        # Open input CSV file as a text file
  3.  
  4. fout = open("Data1.csv", 'w')   # ** You should edit the filename for each filter
  5. limit = 0.15                    # ** You should edit this variable for the various filters you want to apply
  6.  
  7. header = True                   # The first line of CSV file will be the header
  8. profit_per_col = 0
  9. count_in, count_out = 0, 0
  10.  
  11. for fline in fin:                           # Read one line at a time from input CSV file
  12.     count_in += 1                           # count number of lines in input file
  13.     csvlist = fline.split(",")
  14.     if header:                              # If header line ...
  15.         fout.write(fline)                   # Write the header line to the output file
  16.         count_out += 1                      # Count one output line
  17.         header = False                      # mark that header has been processed
  18.         for col, field in enumerate(csvlist):       # get column number and data for each column
  19.             if "profit_per" in field:               # look for "profit_per" in header line
  20.                 profit_per_col = col                # Remember what column is "profit_per"
  21.                 break                               # break out of column loop when we have found the required column
  22.  
  23.         continue                             # Go back in outer loop to get next input line
  24.  
  25.     # From here on we are processing data lines
  26.     profit_per = csvlist[profit_per_col]            # Extract the data from the "profit_per" column
  27.     try:                                            # Try converting this data to a float
  28.         pp_float = float(profit_per)        # Get profit_per as a float if possible
  29.         if pp_float >= limit:               # Check if profit_per passes the filter
  30.             fout.write(fline)               # Write filtered data to output file
  31.             count_out += 1                  # Count output lines
  32.     except:                                 # "profit_per" column did not contain a float value
  33.         continue                            # skip this line, go back to read next line
  34.  
  35. print(f"{count_in=} {count_out=}")          # Print the number of input and output lines in CSV files
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement