我试图通过正则表达式传递大量随机html,我的Python 2.6脚本对此感到窒息:
UnicodeEncodeError:'ascii'编解码器无法编码字符
我在这个词的末尾追溯到商标上标:Protection™ - 我不需要捕获非ascii的东西,但这是一个令人讨厌的东西,我希望将来会更多地遇到它.
是否有处理非ascii字符的模块?或者,在python中处理/转义非ascii内容的最佳方法是什么?
谢谢!完整错误:
E
======================================================================
ERROR: test_untitled (__main__.Untitled)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\Test2.py", line 26, in test_untitled
ofile.write(Test + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 1005: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
完整脚本:
from selenium import selenium
import unittest, time, re, csv, logging
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://www.BaseDomain.com/")
self.selenium.start()
self.selenium.set_timeout("90000")
def test_untitled(self):
sel = self.selenium
spamReader = csv.reader(open('SubDomainList.csv', 'rb'))
for row in …Run Code Online (Sandbox Code Playgroud) 我有一个文件,每小时12,000生成aprox 行6.在其中一些行中,有非ascii字符.
我希望能够运行Perl脚本来删除其中包含非ASCII字符的所有行.
我正在从文本文件中读取文本.文本文件必须读取的第一个字符串是"Algood",并注意spaaaaaace.在记事本中,似乎这个字符串中有一个spaaaaaace,但事实并非如此.当我在Visual Studio的QuickWatch中测试第6个(从零开始的索引)字符时,它显示为:
"?"c
Run Code Online (Sandbox Code Playgroud)
当我使用该Asc函数获取ASCII码时,它告诉我ASCII码是63. 63是一个问号.但是当我测试以查看字符串是否包含ASCII 63时,它会测试为false.所以看起来字符串包含带有ASCII码63的字符,只有它没有,它包含一些其他字符,它们测试为ASCII码63.这是一个问题:如果我不这样做,我就无法删除该字符知道怎么称呼它.我可以删除最后一个字符,但文本文件中的每个字符串都不包含此字符.

问题是:如果不是问号,这个字符是什么,我怎样才能唯一识别所以我可以删除它?
我正在尝试使用支持CP437编码的热敏打印机从诗歌基金会的每日诗歌RSS源中打印一首诗.这意味着我需要翻译一些角色; 在这种情况下,连字符连字符.但python甚至不会编码en dash开头.当我尝试解码字符串并用连字符替换en-dash时出现以下错误:
Traceback (most recent call last):
File "pftest.py", line 46, in <module>
str = str.decode('utf-8')
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 140: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#!/usr/bin/python
#-*- coding: utf-8 -*-
# This string is actually a variable entitled d['entries'][1].summary_detail.value
str = """Love brought by night a vision to my bed,
One that still wore the vesture of a child
But eighteen …Run Code Online (Sandbox Code Playgroud) 我想从网页上读取非ASCII JSON数据,在我的例子中是Perrsian.这是我在python-2.7中的代码:
jsonObject = json.loads(urllib2.urlopen("https://api.instagram.com/v1/users/"+userId+"/?access_token="+accessToken).read().decode('utf-8').encode('utf-8'));
print jsonObject;
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使在decoding和encoding我得到了这样的结果:
{u'meta': {u'code': 200}, u'data': {u'username': u'*******', u'bio': u'\u0639\u06a9\u0633 \u062f\u0648 \u0646\u0641\u0631\u062a\u0648\u0646 \u0631\u0648 \u0627\u0631\u0633\u0627\u0644 \u06a9\u0646\u06cc\u062f\U0001f48f\U0001f491', u'website': u'', u'profile_picture': u'*****', u'full_name': u'\U0001f451\u0639\u0634\u0642 \u0647\u0627\u06cc \u0627\u06cc\u0631\u0627\u0646\u06cc\U0001f451', u'counts': {u'media': 31, u'followed_by': 12449, u'follows': 0}, u'id': u'*******'}}
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能正确获取角色?
如何删除VBA中不属于ASCII类别的所有特殊字符?
这些是我的字符串中出现的一些符号,需要将其删除.ŒœŠšƒ还有更多这样的角色.
这不属于ASCII类别,因为您可以看到这个http://www.ascii.cl/htmlcodes.htm
我试过这样的事
strName = Replace(strName, ChrW(376), " ")
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
请帮我解决这个问题.
谢谢Jeevan
我有一个土耳其语单词列表。我需要比较它们的长度。但是由于一些土耳其语字符是非ASCII,我无法正确比较它们的长度。非 ASCII 土耳其语字符占 2 个字节。
例如:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "ab";
char s2[] = "ç?";
printf("%d\n", strlen(s1)); // it prints 2
printf("%d\n", strlen(s2)); // it prints 4
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的朋友说可以在 Windows 中使用以下代码行做到这一点:
system("chcp 1254");
Run Code Online (Sandbox Code Playgroud)
他说它将土耳其字符填充到扩展的 ASCII 表中。但是它在 Linux 中不起作用。
有没有办法在 Linux 中做到这一点?
我在 Unix 文件系统上有一个奇怪的文件。文件名中似乎有一些特殊字符,但我无法将其删除。即使我没有直接在rm命令中写入名称(ls | rm而是直接写入),我也会收到文件不存在的错误消息。下面是我在互联网上搜索几次后尝试的一些命令,以调试问题。
您对如何删除它有什么建议吗?系统是AIX 7.1。我也试过rm和一个perl脚本(只是列出所有文件并从文件夹中删除所有内容),但没有任何效果。我无法将文件夹移动到/tmp任何一个,我收到相同的错误。
谢谢!
[root@server] ls -1b | od -bc
0000000 342 134 062 060 060 134 062 062 063 012
? \ 2 0 0 \ 2 2 3 \n
0000012
[root@server]$ ls -li
ls: 0653-341 The file ./– does not exist.
total 0
[root@server]$ ls
–
[root@server]$ ls | od -bc
0000000 342 200 223 012
? 200 223 \n
0000004
[root@server]$ rm * …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用getline()命令从用户那里获取重音字符,但它没有正确打印它们。
我尝试将一些库包含为locale,但徒劳无功。
这是我的代码:
#include <iostream>
#include <cstdlib>
#include <string>
#include <locale>
using namespace std;
class Pers {
public:
string name;
int age;
string weapon;
};
int main()
{
setlocale(LC_ALL, "");
Pers pers;
cout << "Say the name of your character: ";
getline(cin, pers.name);
cout << pers.name;
}
Run Code Online (Sandbox Code Playgroud)
当我输入:Mark Coração 时,我得到的是:
我如何解决它?
我正在创建一个视频效果,它应该看起来像“矩阵”电影,但有点不同(类似“矩阵”的视频输出将与真实视频的改变 alpha 通道混合,所以它看起来一半真实,一半带数字)。我只是使用带有 caca 驱动程序的 mplayer (mplayer -vo caca video.mp4) 以及屏幕录制,然后在其他软件中混合视频。为此,我需要更改文件dither.c 中的“静态 uint32_t ascii_glyphs[]”数组(来自此处发布的 caca 库的代码:https : //github.com/cacalabs/libcaca/blob/master/caca/ dither.c ) from: ' ', '.', ':', ';', 't', '%', 'S', 'X', '@', '8', '?'包含所有片假名符号。但问题是看起来它们不可打印。所以视频的终端输出只包含阴影块。我应该说 bash 代码:
str123="???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????"
for i in $(seq 0 ${#str123}); do echo -n "'${str123:i:1}',"; done
Run Code Online (Sandbox Code Playgroud)
在我的终端中正常工作(用几个终端程序检查,正确打印),还设置了语言环境:
$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=
Run Code Online (Sandbox Code Playgroud)
新数组的结果:
/* List of glyphs */
static uint32_t ascii_glyphs[] …Run Code Online (Sandbox Code Playgroud)