【问题标题】:Searching and replacing cent symbol in Python在 Python 中搜索和替换分号
【发布时间】:2014-09-24 04:00:36
【问题描述】:
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")
¢

当我 cat 我的数据文件时,它实际上显示为 ¢ 符号,缺少 Â。

我认为当我尝试使用 Unicode 进行 sed 时,¢ 之前添加的符号不允许我进行搜索和替换。

尝试 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 的 Unicode
  • 如果您能向我们展示您遇到的错误,那就太好了。
  • 如果这个问题解决了,不要在标题中加上 SOLVED ——这不是 Bugzilla。相反,选择最有效的答案,然后单击它旁边的复选标记大纲将其设置为答案。如果两个答案都不起作用,请添加您自己的答案作为答案并改为选中它。

标签: python python-2.7 sed centos6


【解决方案1】:

窃取托马斯的答案并对其进行扩展:

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 

【讨论】:

  • 在这两个例子中,我都得到了这个错误: Traceback (most recent call last): File "test.py", line 53, in hello_unicode = hello_utf8.decode('utf-8' )文件“/usr/local/lib/python2.7/encodings/utf_8.py”,第 16 行,在 decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in位置 517: 无效的起始字节
  • 1) 第 53 行?两个示例都没有第 53 行。 2) 简化数据文件。尝试只有一行文本,看看错误是否仍然存在。 3) 数据文件中偏移量 517 处的数据是什么?
  • 用简化文件再次尝试。该文件有 1 行。请参阅操作以供参考
  • 我找到了答案。问题出在字符集中。我必须将文件从 iso-8895-1 转换为 utf-8,然后我才能使用 sed 进行搜索/替换。
【解决方案2】:

另外,将您的字符串更改为 unicode 字符串,并将分号替换为 \u00A2

这里是固定代码:

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

【讨论】:

  • 感谢您的快速响应,但它似乎仍然无法正常工作。
  • 试试我的新编辑,我改变了一些东西。没有理由这不起作用,因为 Python 会解析字符串文字并将 unicode 分号替换为实际的分号,然后再将其发送到函数。
  • 这是我得到的错误:hello = subprocess.call(u"cat datafile | sed \"s/\uxA2/$0./g\"",shell=True) SyntaxError: (unicode错误)“unicodeescape”编解码器无法解码位置 26-27 中的字节:截断 \uXXXX 转义
  • 已修复,立即尝试。
  • sed: -e expression #1, char 7: 's' 1 的未知选项,这就是我得到的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
相关资源
最近更新 更多