acclivity

pyProcessCSVfile

Jan 26th, 2022 (edited)
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Process a CSV file without using Pandas etc.
  2. # Process a name field, splitting it into first names() and last name
  3.  
  4. # Open input csv file
  5. fin = open('NameFile.csv')
  6.  
  7. # open a new output file
  8. # (I don't like to overwrite my input file, or each time I test my script,)
  9. # (I run the chance of destroying my test data)
  10. fout = open("NewFile.csv", 'w')
  11.  
  12. for ctr, lin in enumerate(fin):     # Read one line of input file, and keep a count of lines (ctr)
  13.     if not ctr:                     # If this is the first line (ctr is zero) ...
  14.         fout.write(lin)             # ... just copy the input to output
  15.         continue                    # ... and loop back to read the next line of input file
  16.  
  17.     lin = lin.strip() + ",,,"       # Remove white space from both ends, then ensure there are 4 fields
  18.     lsplit = lin.split(',')         # Create a list of fields from the input line
  19.     name = lsplit[0]                # Extract the first field which is the full name
  20.     if not name:                    # If no name is present ...
  21.         continue                    # ... ignore this line
  22.     for c in "-&/+":                # Check for any special characters in the full name
  23.         if c in name:               # If a special character exists ...
  24.             lsplit[2] = name        # ... put the full name into the 3rd output field (Last Name)
  25.             break                   # ... and exit the loop
  26.     else:                           # If the last loop did NOT end with a break ...
  27.                                     # ... then there were no special characters present
  28.         namesplit = name.split(' ')     # Split the full name into a list of parts
  29.  
  30.         lsplit[2] = namesplit.pop()         # remove the final name part from the list ...
  31.                                             # ... and put it in the Last Name field
  32.                                    
  33.         lsplit[1] = " ".join(namesplit)     # Join together any earlier name parts with space joiner
  34.                                             # ... and put the result into First Name field
  35.  
  36.     lineout = ",".join(lsplit[:3])      # Create a CSV line from the first 3 fields
  37.     fout.write(lineout + "\n")          # Write the output line (with EOL) to output file
  38.  
  39.     # End of loop. Go back to read next line from input file
  40.  
RAW Paste Data Copied