在python字符串中,如何将字符“œ”替换为“£”

时间:2022-09-13 09:23:36

i am parsing a CSV file into python but need to replace the œ charachter with £, how do i achieve this as adding:

我正在将一个CSV文件解析为python,但是需要用£替换-charachter,我如何实现这个添加:

#!/usr/bin/python
# -*- coding: cp1252 -*-

does not work.

不起作用。

i see the "œ" sign in the CSV file, when parsed by python it reads as "œ", so i need this "œ" replaced with "£" by python.

我在CSV文件中看到“œ”符号,当被python解析时,它显示为“œ”,所以我需要用python将“œ”替换为“£”。

the CSV is opened using: data = [row for row in csv.reader(open(CSVfilename, 'rU'))] i then have: testVar = (data[someRow][someCol]).replace(",", "") which essentially removes commas, but when i add: replace.("œ", "£") it does not work

使用以下命令打开CSV:data = [csv.reader中的行的行(打开(CSVfilename,'rU'))]然后我有:testVar =(data [someRow] [someCol])。replace(“,”,“ “)基本上删除了逗号,但是当我添加:replace。(”œ“,”£“)时它不起作用

can someone please help? thanks

有人可以帮忙吗?谢谢

1 个解决方案

#1


The coding: cp1252 is how the python file you are working on is encoded, which has nothing to do with the encoding of your CSV file.

编码:cp1252是你编写的python文件的编码方式,与CSV文件的编码无关。

I guess your problem is that you are not opening the csv taking into account the right encoding, and therefore you are getting weird characters like œ instead of £. If this is the problem: find out how the file was encoded, and open it with that encoding.

我猜你的问题是你没有考虑正确的编码打开csv,因此你会得到奇怪的字符,比如œ而不是£。如果这是问题:找出文件的编码方式,并使用该编码打开它。

If however, you are opening properly the file, and you want to change the contents from œ to £, you just have to perform a string replacement. Example as following (naive approach, not very memory efficient, and asssuming utf-8)

但是,如果您正在正确打开文件,并且想要将内容从œ更改为£,则只需执行字符串替换即可。示例如下(天真的方法,不是非常有效的内存,并且假设utf-8)

with open(csv_path) as csv_file:
    csv_contents = csv_file.read().decode('utf-8')
    csv_contents = csv_contents.replace(u'œ', u'£')
    # Do something with your csv...

#1


The coding: cp1252 is how the python file you are working on is encoded, which has nothing to do with the encoding of your CSV file.

编码:cp1252是你编写的python文件的编码方式,与CSV文件的编码无关。

I guess your problem is that you are not opening the csv taking into account the right encoding, and therefore you are getting weird characters like œ instead of £. If this is the problem: find out how the file was encoded, and open it with that encoding.

我猜你的问题是你没有考虑正确的编码打开csv,因此你会得到奇怪的字符,比如œ而不是£。如果这是问题:找出文件的编码方式,并使用该编码打开它。

If however, you are opening properly the file, and you want to change the contents from œ to £, you just have to perform a string replacement. Example as following (naive approach, not very memory efficient, and asssuming utf-8)

但是,如果您正在正确打开文件,并且想要将内容从œ更改为£,则只需执行字符串替换即可。示例如下(天真的方法,不是非常有效的内存,并且假设utf-8)

with open(csv_path) as csv_file:
    csv_contents = csv_file.read().decode('utf-8')
    csv_contents = csv_contents.replace(u'œ', u'£')
    # Do something with your csv...