简体   繁体   中英

Finding and replacing values in specific columns in a CSV file using dictionaries

My Goal here is to clean up address data from individual CSV files using dictionaries for each individual column. Sort of like automating the find and replace feature from excel. The addresses are divided into columns. Housenumbers , streetnames , directions and streettype all in their own column. I used the following code to do the whole document.

missad = {
'Typo goes here': 'Corrected typo goes here'}

def replace_all(text, dic):
for i, j in missad.items():
    text = text.replace(i, j)
return text

with open('original.csv','r') as csvfile:
text=csvfile.read()
text=replace_all(text,missad)

with open('cleanfile.csv','w') as cleancsv:
cleancsv.write(text)

While the code works, I need to have separate dictionaries as some columns need specific typo fixes.For example for the Housenumbers column housenum , stdir for the street direction and so on each with their column specific typos:

housenum = {
'One': '1',
'Two': '2
}
stdir = {
'NULL': ''}

I have no idea how to proceed, I feel it's something simple or that I would need pandas but am unsure how to continue. Would appreciate any help! Also is there anyway to group the typos together with one corrected typo? I tried the following but got an unhashable type error.

missad = { ['Typo goes here',Typo 2 goes here',Typo 3 goes here']: 'Corrected typo goes here'}

is something like this what you are looking for?

import pandas as pd

df = pd.read_csv(filename, index_col=False)   #using pandas to read in the CSV file

#let's say in this dataframe you want to do corrections on the 'column for correction' column

correctiondict= {
                  'one': 1,
                  'two': 2
                 }

df['columnforcorrection']=df['columnforcorrection'].replace(correctiondict)

and use this idea for other columns of interest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM