Skip to content Skip to sidebar Skip to footer

Change Values Of A Column In CSV File Using Python

I am new to python and just need a small help. We have a Pipe delimited CSV file which looks like this DATE|20160101 ID | Name | Address | City | State | Zip | Phone | OPEID |

Solution 1:

You can do it very efficiently with Pandas--this will be good if your file is very large:

import pandas as pd
import sys

with open('t.txt') as infile:
    title = next(infile)
    infile.seek(0)
    table = pd.read_csv(infile, '|', header=1, dtype=str)

table.rename(columns={'Unnamed: 9':''}, inplace=True)

table[' Zip   '] = table[' Zip   '].str.replace("'", "")
table[' OPEID  '] = table[' OPEID  '].str.replace("'", "")

sys.stdout.write(title)
table.to_csv(sys.stdout, '|', index=False)

Solution 2:

It worked for me... Try this.

res=[]
with open('hi.csv') as f:
    content=csv.reader(f,delimiter='|')
    for row in content:
        for str in range (len(row)):
            row[str]=row[str].replace('\'','')
        res.append(row)
    f.close()
with open('hi.csv','wb') as ff:  # python 3 => 'wb' => 'w',newline=''
    sw=csv.writer(ff,delimiter='|',quoting=csv.QUOTE_MINIMAL)
    for rows in res:
        sw.writerow(rows)
ff.close()

Solution 3:

To remove apostrophes you can use the replace function, you just need to get the content of every cell one by one, and replace the apostrophes with:

new = old.replace("'", "")

More simply, open your csv file with any file editor and search and replace for "'".


Solution 4:

The code below would be the same for all file formats. The fact that it is a *.csv doesn't change a thing. What it actually does, is that it goes in the file from which you want to remove the apostrophes, my_csv_in, and parses it line by line each time replacing them with nothing (a.k.a removing). The modified lines are written in a second file, my_csv_out.

my_csv_in = r'full_file_path_to_csv_in.csv'
my_csv_out = r'full_file_path_to_csv_out.csv'

with open(my_csv_in, 'r') as f_in:
    with open(my_csv_out, 'w') as f_out:
        for line in f_in:
            f_out.write(line.replace("'", ''))

There are probably better ways to do this that take advantage of the file being a *.csv and using the csv library. You can take a look at the quoting options in the documentation.


Post a Comment for "Change Values Of A Column In CSV File Using Python"