繁体   English   中英

在Python中搜索和替换分符号

[英]Searching and replacing cent symbol in Python

OS: CentOS 6.5
Python version: 2.7.5

我有一个包含以下信息样本的文件。 我想搜索并替换分符号并替换为$ 0。 在前。

Alpha $1.00
Beta  ¢55  <<<< note
Charlie $2.00
Delta  ¢23  <<<< note

我希望它看起来像这样:

Alpha $1.00
Beta  $0.55  <<<< note
Charlie $2.00
Delta  $0.23  <<<< note

因此,命令行中的此代码(有效)为:

sed 's/¢/$0./g' *file name*

但是,使用python进行编码无法正常工作:

import subprocess
hello = subprocess.call('cat datafile ' + '| sed "s/¢/$0./g"',shell=True)
print hello

每当我尝试粘贴¢符号时,似乎都会出现错误。

稍微靠近一点,当我在Python中为百分号打印unicode时,结果如下:

print(u"\u00A2")
¢

当我整理数据文件时,它实际上显示为¢符号,缺少Â。 <<不确定这是否有帮助

我想当我尝试使用Unicode时,在¢之前添加的符号不允许我搜索和替换。

尝试unicode时出现错误代码:

hello = subprocess.call(u"cat datafile | sed 's/\uxA2/$0./g'",shell=True)
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 25-26: truncated \uXXXX escape

将uxA2固定为u00A2,我得到以下信息:

sed: -e expression #1, char 7: unknown option to `s'
1

有什么想法/想法吗?

这两个示例都出现以下错误:

[root@centOS user]# python test2.py
Traceback (most recent call last):
  File "test2.py", line 3, in <module>
    data = data.decode('utf-8')             # decode immediately to Unicode
  File "/usr/local/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa2 in position 6: invalid start byte

[root@centOS user]# python test1.py
Traceback (most recent call last):
  File "test1.py", line 11, in <module>
    hello_unicode = hello_utf8.decode('utf-8')
  File "/usr/local/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa2 in position 6: invalid start byte

这是文件的猫:

[root@centOS user]# cat datafile
alpha ¢79 

这是数据文件的Nano:

alpha �79

这是数据文件的Vim:

[root@centOS user]# vim fbasdf
alpha ¢79
~

再次感谢您的帮助

回答!!

Rob和Thomas的SED输出起作用。 文件格式另存为charset = iso-8859-1。 我无法在文档中搜索utf-8格式字符。

识别的文件字符集:

file -bi datafile
text/plain; charset=iso-8859-1

使用以下代码更改文件:

iconv -f iso-8859-1 -t utf8 datafile > datafile1

窃取Thomas的答案并扩大其范围:

import subprocess

# Keep all strings in unicode as long as you can.
cmd_unicode = u"sed 's/\u00A2/$0./g' < datafile"

# only convert them to encoded byte strings when you send them out
# also note the use of .check_output(), NOT .call()
cmd_utf8 = cmd_unicode.encode('utf-8')
hello_utf8 = subprocess.check_output(cmd_utf8, shell=True)

# Decode any incoming byte string to unicode immediately on receipt
hello_unicode = hello_utf8.decode('utf-8')

# And you have your answer
print hello_unicode

上面的代码演示了“ Unicode三明治”的用法:外部字节,内部Unicode。 参见http://nedbatchelder.com/text/unipain.html

对于这个简单的示例,您可以轻松地完成Python中的所有操作:

with open('datafile') as datafile:
    data = datafile.read()              # Read in bytes
data = data.decode('utf-8')             # decode immediately to Unicode
data = data.replace(u'\xa2', u'$0.')    # Do all operations in Unicode
print data                              # Implicit encode during output 

另外,将您的字符串更改为unicode字符串,并用替换

这是固定代码:

import subprocess
hello = subprocess.call(u"cat datafile | sed \"s#\u00A2#$0.#g\"",shell=True)
print hello

暂无
暂无

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

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