简体   繁体   English

Python 用 CSV 中的特殊字符替换字符串

[英]Python Replace String with Special Characters in a CSV

I'm trying to replace multiple strings (with multiple languages characters) in a csv file.我正在尝试替换 csv 文件中的多个字符串(具有多种语言字符)。 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.在此过程中,以下代码工作假设如果我将 csv 文件重命名为 .txt 扩展名,然后重命名回 .csv 扩展名。 I'm wondering if directly csv can be read and write.我想知道是否可以直接读写csv。

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. csv 文件只不过是一个简单的 txt 文件,它旨在通过用逗号分隔值来表示数据表。 This allows programs to read it efficiently into data using libraries like Python's csv .这允许程序使用 Python 的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.由于它仍然只是一个文本文件,因此您也可以像open一样使用简单的 function 将其作为普通 txt 文件打开,并以与使用 txt 文件完全相同的方式使用它。

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.您可以调用一个文本文件myfile.kingkong ,它的行为仍然与open的 function 相同。 In the same way, renaming .csv to .txt does absolutely nothing.同样,将.csv重命名为.txt完全没有任何作用。

Use pandas .使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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