Home>

I want to edit csv with python.
What should I do to replace the character string "aaa" with the character string "AAA" in the table format as shown below?

| column 1 | column 2 | column 3 | column 4 | column 5 | column 6 | column 7 | column 8 | column 9 | column 10 | column 11 | column 12 | column 13 | column 14 |
| aaa | aaa | bb | vv | aaa | aaa | aaa | aaa | ddd | ggg | aaa | aaa | aaa | fff |
| w | aa | f | g | aaa | a | aa || gfg | fff | eee | ggg | aaa | aaa | ggg |

  • Answer # 1

    csv is a text file and can be processed in the same way as normal text editing.

    Read the entire file as a string → Edit with methods such asstr.replace ()→ Export to the same file

    If the flow

    is written in the program, that should be fine.

    Addendum
    with open (file name, "r") as f:
        s = f.read ()
    s = s.replace ("aaa", "AAA")
    with open (filename, "w") as f:
        f.write (s)

  • Answer # 2

    No need for Python. Yes, shell art!

    $sed -i 's/aaa/AAA/g' file.csv