Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Process a CSV file without using Pandas etc.
- # Process a name field, splitting it into first names() and last name
- # Open input csv file
- fin = open('NameFile.csv')
- # open a new output file
- # (I don't like to overwrite my input file, or each time I test my script,)
- # (I run the chance of destroying my test data)
- fout = open("NewFile.csv", 'w')
- for ctr, lin in enumerate(fin): # Read one line of input file, and keep a count of lines (ctr)
- if not ctr: # If this is the first line (ctr is zero) ...
- fout.write(lin) # ... just copy the input to output
- continue # ... and loop back to read the next line of input file
- lin = lin.strip() + ",,," # Remove white space from both ends, then ensure there are 4 fields
- lsplit = lin.split(',') # Create a list of fields from the input line
- name = lsplit[0] # Extract the first field which is the full name
- if not name: # If no name is present ...
- continue # ... ignore this line
- for c in "-&/+": # Check for any special characters in the full name
- if c in name: # If a special character exists ...
- lsplit[2] = name # ... put the full name into the 3rd output field (Last Name)
- break # ... and exit the loop
- else: # If the last loop did NOT end with a break ...
- # ... then there were no special characters present
- namesplit = name.split(' ') # Split the full name into a list of parts
- lsplit[2] = namesplit.pop() # remove the final name part from the list ...
- # ... and put it in the Last Name field
- lsplit[1] = " ".join(namesplit) # Join together any earlier name parts with space joiner
- # ... and put the result into First Name field
- lineout = ",".join(lsplit[:3]) # Create a CSV line from the first 3 fields
- fout.write(lineout + "\n") # Write the output line (with EOL) to output file
- # End of loop. Go back to read next line from input file
Add Comment
Please, Sign In to add comment