Python Forum
How to do a mass replace within a CSV file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do a mass replace within a CSV file?
#1
I have the following code that im using to open a CSV and then create a new filtered csv for importing into excel.

# THIS OPENS THE NEWLY CLEAN DATA IN ORDER TO REMOVE OLD RECORDS
df = pd.read_csv("Dates.txt", usecols=range(3), header=None, names=["Date", "Time", "Comment"])
# THIS CONVERTS THE DATE COLUMN INTO A DATETIME FORMAT
df['DT'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
# HERE YOU NEED TO PROVIDE THE DATE YOU WANT TO KEEP GOING FORWARD
mask = (df['DT'] > '2022-04-28  7:06 AM')
# THIS RETURNS ALL ROWS GREATER THAN THE DATE PROVIDED ABOVE
ndf = df.loc[mask]
The above works great and does what is needed, NOW we are trying to do a mass replace for a key string value so that it can update the pivot table we have in excel when the data is imported.

In the 3rd column of the file ("Comment") there is a value for each record that has 2 different values based on the row, so just need to replace this string:

Someone Else:    

with

Completed:
And replace this string:

Davids:

with

Dave:
Is it possible to do a find and replace or do we have to do it per line?

Currently we open the file in NotePad++ and just do a find and replace, but would love to have that built into the creation of the new file so we dont have to remember to do that step.
Reply


Messages In This Thread
How to do a mass replace within a CSV file? - by cubangt - May-06-2022, 07:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 644 May-01-2025, 03:43 PM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 2,453 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Replace a text/word in docx file using Python Devan 4 25,850 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 2,075 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 1,874 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 2,193 May-06-2022, 01:44 PM
Last Post: Larz60+
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 3,002 May-31-2021, 07:00 AM
Last Post: bowlofred
  So, a mass of errors trying to run a zipline-dependant program on 3.5 env Evalias123 2 3,429 Jan-21-2021, 02:22 AM
Last Post: Evalias123
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 9,577 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Replace XML tag and write to the same file krish216 4 11,035 Mar-26-2020, 07:39 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020