简体   繁体   中英

Python Replace String with Special Characters in a CSV

I'm trying to replace multiple strings (with multiple languages characters) in a csv file. The code got so messed up that I couldn't simply ask to correct my mistakes.

In the process, Following code works assuming that if I rename csv files with.txt extension and then rename back to.csv extension. I'm wondering if directly csv can be read and write.

import io

match = {
    "太好奇了": "First String",
    "धेरै एक्लो": "Second String",
    "심각하게 생명이 필요하다": "Third String"
}

f = io.open("input.txt", mode="r", encoding="utf-16")
data = f.read()


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


data = replace_all(data, match)
w = open("updated.txt", "w", encoding="utf-16")
w.write(data)

A csv file is nothing else than a simple txt file that is meant to represent a data table by separating the values by commas. This allows programs to read it efficiently into data using libraries like Python's csv .

Since it still is just a text file, you can also open it as a usual txt using a simple function like open and use it the exact same way you would use a txt file.

f = open("myfile.csv", mode="r", encoding="utf-16")
data = f.read()
f.close()

Note that file extensions actually change nothing about the file, they just signal how the file should be used. You could call a text file myfile.kingkong and it would still behave the same with the open function. In the same way, renaming .csv to .txt does absolutely nothing.

Use pandas .

Here's some code to help you get started.

import pandas as pd

filename = 'data.csv'

df = pd.read_csv(filename, encoding='utf-16')

# this will replace "太好奇了" with "First String"
df.replace(to_replace="太好奇了", value="First String")

df.to_csv('update.csv') # save result

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