简体   繁体   中英

How to replace string character pattern using python in csv file

I am new to python. How to replace string character ," to ,{ and ", to }, which contains multilines in .csv file?

Here is my content of .csv file

Name, Degree,Some, Occupation, Object
Mr. A,"B.A, M.A",123,"ags,gshs",ass
Mr. ABC,"B.A, M.A",231,"ags,gshs",asas
Mr. D,"BB.A, M.A",44,"ags,gshs",asas
Mr. G,"BBBB.A, M.A",12,"ags,gshs",asasasa
Mr. S,"B.A, MMM.A",10,"ags,gshs",asasas
Mr. R,"B.A, M.A",11,"ags,gshs",asasas
Mr. T,"B.A, M.A",12,"ags,gshs",asasa
Mr. DD,"B.A, M.A",13,"ags,gshs",asasas

So my output will be something like this

 Name, Degree,Some, Occupation, Obejct
 Mr. A,{B.A, M.A},123,{ags,gshs},ass
 Mr. ABC,{B.A, M.A},231,{ags,gshs},asas
 Mr. D,{BB.A, M.A},44,{ags,gshs},asas
 Mr. G,{BBBB.A, M.A},12,{ags,gshs},asasasa
 Mr. S,{B.A, MMM.A},10,{ags,gshs},asasas
 Mr. R,{B.A, M.A},11,{ags,gshs},asasas
 Mr. T,{B.A, M.A},12,{ags,gshs},asasa
 Mr. DD,{B.A, M.A},13,{ags,gshs},asasas

After opening the file with file.read() , you can use replace(old, new) to replace the string characters you desire. Keep in mind, since the strings ," and ", contain quotes, you must put a \\ before the quotes to show they part of the string.

EDIT: A comment mentioned you could enclose the string in ' ' . If you do this, putting \\ before the quotes is not required. For example, both ",\\"" and ',"' are valid strings.

data = ""

with open("/path/to/file.csv") as file:
     data = file.read().replace(",\"", ",{").replace("\",", "},")

with open("/path/to/new_file.csv") as file:
     file.write(data)

If you only need it once you could use pandas like this:

import pandas as pd

data1 = '''\
Name,Degree,Some,Occupation,Object
Mr. A,"B.A, M.A",123,"ags,gshs",ass
Mr. ABC,"B.A, M.A",231,"ags,gshs",asas
Mr. D,"BB.A, M.A",44,"ags,gshs",asas
Mr. G,"BBBB.A, M.A",12,"ags,gshs",asasasa
Mr. S,"B.A, MMM.A",10,"ags,gshs",asasas
Mr. R,"B.A, M.A",11,"ags,gshs",asasas
Mr. T,"B.A, M.A",12,"ags,gshs",asasa
Mr. DD,"B.A, M.A",13,"ags,gshs",asasas'''

df = pd.read_csv(pd.compat.StringIO(data1), sep=',', dtype=object)
#df = pd.read_csv('input.csv', sep=',', dtype=object)  # Use this row for real application

df['Degree'] = '{'+df['Degree']+'}'
df['Occupation'] = '{'+df['Occupation']+'}'

# Create custom output
out = '\n'.join([','.join(df.columns), '\n'.join(','.join(i) for i in df.values)])

with open('output.csv') as f:
    f.write(out)

You can use unpacking:

import csv
with open('filename.csv') as f:
  data = filter(None, list(csv.reader(f)))
  with open('filename.csv', 'w') as f1:
    write = csv.writer(f1)
    write.writerows([data[0]]+[[a, '{'+b+'}', c, '{'+d+'}', e] for a, b, c, d, e in data[1:]])

Output:

Name, Degree,Some, Occupation, Object
Mr. A,{B.A, M.A},123,{ags,gshs},ass
Mr. ABC,{B.A, M.A},231,{ags,gshs},asas
Mr. D,{BB.A, M.A},44,{ags,gshs},asas
Mr. G,{BBBB.A, M.A},12,{ags,gshs},asasasa
Mr. S,{B.A, MMM.A},10,{ags,gshs},asasas
Mr. R,{B.A, M.A},11,{ags,gshs},asasas
Mr. T,{B.A, M.A},12,{ags,gshs},asasa
Mr. DD,{B.A, M.A},13,{ags,gshs},asasas

Try:

def find_replace(csv_path, search_characters, replace_with):
    text = open(csv_path, "r")
    text = ''.join([i for i in text]).replace(
        search_characters, replace_with)
    x = open(csv_path, "w")
    x.writelines(text)
    x.close()


if __name__ == '__main__':
    csv_path = "path/to/csv/file.csv"
    search_characters = ',"'
    replace_with = ',{'

    find_replace(csv_path, search_characters, replace_with)

    search_characters = '",'
    replace_with = '},'

    find_replace(csv_path, search_characters, replace_with)

The above code opens the file, writes some data to it and then closes it.

Or, if you prefer list s as well the with statement which will take care to call __exit__ function of the given object even if something bad happened in code.

def find_replace(csv_path, search_characters, replace_with):
    s_one, s_two = search_characters
    r_one, r_two = replace_with
    with open(csv_path) as file:
        data = file.read().replace(s_one, r_one).replace(s_two, r_two)
    with open(csv_path, 'w') as file:
        file.write(data)

if __name__ == '__main__':
    csv_path = "path/to/csv/file.csv"
    
    search_characters = [',"', '",']
    replace_with = [',{', '},']

    find_replace(csv_path, search_characters, replace_with)

The main advantage of using a with statement is that it makes sure our file is closed without paying attention to how the nested block exits.

Tested and works nicely on your example.

Replacing string character pattern using python in csv file

text = open("input.csv", "r")
text = ''.join([i for i in text]).replace("character to be replaced", "character to be replaced with")
x = open("output.csv","w")
x.writelines(text)
x.close()

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