我正在尝试打印一个没有特定编码十六进制的unicode字符串.我从facebook获取这些数据,在UTF-8的html标题中有一个编码类型.当我打印类型 - 它说它的unicode,但是当我尝试使用unicode-escape解码它时说有一个编码错误.为什么我在使用解码方法时会尝试编码?
码
a='really long string of unicode html text that i wont reprint'
print type(a)
>>> <type 'unicode'>
print a.decode('unicode-escape')
>>> Traceback (most recent call last):
File "scfbp.py", line 203, in myFunctionPage
print a.decode('unicode-escape')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 1945: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud) 我已安装pyzo并miniconda在Windows 10下安装numpy和matplotlib使用conda install.但是当我想跑的时候
import numpy as np
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
Traceback (most recent call last):
File "<tmp 1>", line 3, in <module>
import numpy
File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__init__.py", line 165, in <module>
from numpy.__config__ import show as show_config
File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__config__.py", line 5
lapack_mkl_info={'libraries': ['mkl_lapack95_lp64', 'mkl_core_dll', 'mkl_intel_lp64_dll', 'mkl_intel_thread_dll'], 'define_macros': [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)], 'include_dirs': ['c:\users\jakub\miniconda3\\Library\\include'], 'library_dirs': ['c:\users\jakub\miniconda3\\Library\\lib']}
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX …Run Code Online (Sandbox Code Playgroud) 我正在尝试从在此链接中找到的 .txt 文件构建一个语料库。我相信 的实例\xad应该是“软连字符”,但在 UTF-8 编码下似乎无法正确读取。我尝试iso8859-15使用以下代码将 .txt 文件编码为:
with open('Harry Potter 3 - The Prisoner Of Azkaban.txt', 'r',
encoding='iso8859-15') as myfile:
data=myfile.read().replace('\n', '')
data2 = data.split(' ')
Run Code Online (Sandbox Code Playgroud)
这将返回一个 'words' 数组,但 '\xad' 仍然附加到 data2 中的许多条目。我试过了
data_clean = data.replace('\\xad', '')
Run Code Online (Sandbox Code Playgroud)
和
data_clean = data.replace('\\xad|\\xad\\xad','')
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有删除 '\xad' 的实例。有没有人遇到过类似的问题?理想情况下,我想将此数据编码为 UTF-8 以使用该nltk库,但它不会读取具有 UTF-8 编码的文件,因为我收到以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 471: invalid start byte
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
附加上下文:这是一个娱乐项目,旨在能够基于 txt 文件生成故事。到目前为止,我生成的所有内容都充满了 '\xad',这破坏了乐趣!
在Python 3中,假设我有
>>> thai_string = '???'
Run Code Online (Sandbox Code Playgroud)
使用encode给出
>>> thai_string.encode('utf-8')
b'\xe0\xb8\xaa\xe0\xb8\xb5'
Run Code Online (Sandbox Code Playgroud)
我的问题:如何使用而不是使用encode()返回bytes序列?我怎么能回到Python 3 类型呢?\u\xdecodestr
我尝试使用ascii内置,这给了
>>> ascii(thai_string)
"'\\u0e2a\\u0e35'"
Run Code Online (Sandbox Code Playgroud)
但这似乎不太正确,因为我无法解码它以获得thai_string.
Python文档告诉我
\xhh转义字符与十六进制值hh,而\uxxxx 使用16位十六进制值转义字符 xxxx文档说\u只用在字符串文字中,但我不确定这意味着什么.这是否暗示我的问题有一个有缺陷的前提?
在Rust中,通过这样做可以从字节中获取UTF-8:
if let Ok(s) = str::from_utf8(some_u8_slice) {
println!("example {}", s);
}
Run Code Online (Sandbox Code Playgroud)
这可能有效,也可能没有,但Python有能力处理错误,例如:
s = some_bytes.decode(encoding='utf-8', errors='surrogateescape');
Run Code Online (Sandbox Code Playgroud)
在此示例中,参数surrogateescape将无效的utf-8序列转换为转义码,因此它们不是忽略或替换无法解码的文本,而是替换为有效的字节文字表达式utf-8.请参阅:Python文档了解详细信息.
Rust是否有办法从字节中获取UTF-8字符串,从而逃避错误而不是完全失败?
JavaTokenParsersScala 中提供了方便的正则表达式来匹配整数和浮点数以及双引号字符串。但这就是它的全部作用。我如何做将这些字符串转换回底层转换对象的明显事情?toDouble对于数字来说,使用or等很容易做到这一点toInt。但是如何对字符串进行等效操作呢?例如,如果我输入字符串
"Unicode \u20ac is a Euro sign, which I would write \\u20ac in a string. \243 is a pound sign.\n\r And \f is a \"form feed\", with embedded quotes.\n\r"
Run Code Online (Sandbox Code Playgroud)
然后我运行这个JavaTokenParsers,我会及时得到一个字符串,该字符串正确解析嵌入的引号,但有一个双引号字符作为其第一个和最后一个字符,以及许多反斜杠序列。如何获得处理了转义序列的等效 Java 字符串?我不敢相信没有库函数可以做到这一点,但找不到。
我在python代码中有一个unicode字符串:
name = u'Mayte_Martín'
Run Code Online (Sandbox Code Playgroud)
我想将它与SPARQL查询一起使用,这意味着我应该使用'utf-8'对字符串进行编码,并在其上使用urllib.quote_plus或requests.quote.但是,这两个引用函数的行为都很奇怪,因为在使用和不使用'safe'参数时可以看到.
from urllib import quote_plus
Run Code Online (Sandbox Code Playgroud)
没有'安全'的论点:
quote_plus(name.encode('utf-8'))
Output: 'Mayte_Mart%C3%ADn'
Run Code Online (Sandbox Code Playgroud)
有了'安全'的说法:
quote_plus(name.encode('utf-8'), safe=':/')
Output:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-164-556248391ee1> in <module>()
----> 1 quote_plus(v, safe=':/')
/usr/lib/python2.7/urllib.pyc in quote_plus(s, safe)
1273 s = quote(s, safe + ' ')
1274 return s.replace(' ', '+')
-> 1275 return quote(s, safe)
1276
1277 def urlencode(query, doseq=0):
/usr/lib/python2.7/urllib.pyc in quote(s, safe)
1264 safe = always_safe + safe
1265 _safe_quoters[cachekey] = (quoter, safe)
-> 1266 if not s.rstrip(safe):
1267 return …Run Code Online (Sandbox Code Playgroud) 我每天都使用大型 pandas 数据帧,它获取我们从网络本地的 webAPI(xml 编码为 utf-8)解析的馈送信息。
在我提供数据帧并导出为 csv 文件后,我开始出现编码错误(本地计算机是 cp1252),在过去的几周里我不得不处理这个错误。
我最终找到的解决方案是在tangfucious的回复下[这里][1]。
df['crumbs'] = df['crumbs'].map(lambda x: x.encode('unicode-escape').decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
一行代码,接受一个字符串并使用 对其进行编码.encode=('unicode_escape'),然后解码为 utf-8。
有人可以向我解释一下这段代码是如何工作的吗?不幸的是,我是一个新手,所以我无法对他的回复发表评论
unicode-escape 的幕后目的是什么(除了明显的,向每个 unicode 代码点添加 \ )。这对解码为 utf-8 有何影响?为什么这是必要的?使用相同的编码进行编码/解码不是总是更好吗?
使用“unicode_escape”还有其他用途吗?
根据文档,unicode 字符应在 strings.xml 文件中与 \\u 前缀一起使用。例如使用\\u00a9 而不是\xc2\xa9.
<string name="copyright_text">Copyrighted \xc2\xa9</string>\nRun Code Online (Sandbox Code Playgroud)\n但如果我直接写 \xc2\xa9 ,即使在 API 21 中,它似乎也能正常工作。除了其他更有影响力的字符(如 ash &、< > 等)之外,是否真的需要转义?
\nunicode android unicode-escapes android-resources android-resource-file
当尝试编织我的 rmd 文件时,我收到此错误 -
! LaTeX Error: Unicode character ^^[ (U+001B)
not set up for use with LaTeX.
Error: LaTeX failed to compile Task1-Final.tex. See
https://yihui.org/tinytex/r/#debugging for debugging tips. See Task1-Final.log for
more info.
Execution halted
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
unicode-escapes ×10
python ×5
unicode ×4
utf-8 ×3
decode ×2
android ×1
dataframe ×1
encode ×1
encoding ×1
escaping ×1
knitr ×1
matplotlib ×1
numpy ×1
parsing ×1
python-3.x ×1
r ×1
r-markdown ×1
rust ×1
scala ×1
sparql ×1
str-replace ×1
tinytex ×1
urllib ×1