我在同一目录中有两个文件:
阿拉伯语 WordNet 的 xml 数据库 ( http://nlp.lsi.upc.edu/awn/get_bd.php ) upc_db.xml
当我尝试运行 .py 文件以给出图像中的错误时,我试图检查 .py 文件是否正常工作,以便我可以将其导入为阿拉伯语单词的 WordNet
你能帮我解决这个问题吗?
谢谢
我在将常规文件读入我已编写的程序时遇到一些麻烦.我目前遇到的问题是pdf基于某种突变的utf-8,其中包括一个BOM,它会在我的整个操作中引发一个问题.在我的应用程序中,我正在使用需要ascii输入的Snowball词干算法.有许多主题涉及到为utf-8解决错误,但是没有一个涉及将它们发送到Snowball算法,或者考虑ascii是我想要的最终结果.目前我使用的文件是使用标准ANSI编码的记事本文件.我得到的具体错误信息是这样的:
File "C:\Users\svictoroff\Desktop\Alleyoop\Python_Scripts\Keywords.py", line 38, in Map_Sentence_To_Keywords
Word = Word.encode('ascii', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我的理解是在python中,包括ignore参数只会传递遇到的任何非ascii字符,这样我就会绕过任何BOM或特殊字符,但显然不是这种情况.调用的实际代码在这里:
def Map_Sentence_To_Keywords(Sentence, Keywords):
'''Takes in a sentence and a list of Keywords, returns a tuple where the
first element is the sentence, and the second element is a set of
all keywords appearing in the sentence. Uses Snowball algorithm'''
Equivalence = stem.SnowballStemmer('english')
Found = []
Sentence = re.sub(r'^(\W*?)(.*)(\n?)$', r'\2', Sentence)
Words = Sentence.split()
for …Run Code Online (Sandbox Code Playgroud) 以下是示例代码,目的只是合并来自give文件夹及其子文件夹的文本文件.我偶尔会得到Traceback所以不知道在哪里看.还需要一些帮助来增强代码以防止空行被合并并在合并/主文件中不显示任何行.在合并文件之前,可能需要进行一些清理或者在合并过程中忽略空行,这可能是个好主意.
文件夹中的文本文件不超过1000行,但聚合主文件可以非常容易地跨越10000多行.
import os
root = 'C:\\Dropbox\\ans7i\\'
files = [(path,f) for path,_,file_list in os.walk(root) for f in file_list]
out_file = open('C:\\Dropbox\\Python\\master.txt','w')
for path,f_name in files:
in_file = open('%s/%s'%(path,f_name), 'r')
# write out root/path/to/file (space) file_contents
for line in in_file:
out_file.write('%s/%s %s'%(path,f_name,line))
in_file.close()
# enter new line after each file
out_file.write('\n')
with open('master.txt', 'r') as f:
lines = f.readlines()
with open('master.txt', 'w') as f:
f.write("".join(L for L in lines if L.strip()))
Traceback (most recent call last):
File "C:\Dropbox\Python\master.py", line 9, in …Run Code Online (Sandbox Code Playgroud) 我查看了类似问题的一些答案,但似乎无法看到我的代码中出现错误的位置.当我尝试运行芹菜时发生错误:celery –A runLogProject worker –loglevel=INFO.我注释掉了与Celery相关的所有代码,它也给出了同样的错误.我也尝试重新安装芹菜.我不知道还有什么可以尝试的.这是追溯:
C:\Python27\lib\site-packages\celery\bin\celery.py:803: UnicodeWarning: Unicode equal comparison failed to convert both
arguments to Unicode - interpreting them as being unequal
if len(argv) > 1 and 'worker' in argv[0:3]:
C:\Python27\lib\site-packages\celery\bin\celery.py:765: UnicodeWarning: Unicode equal comparison failed to convert both
arguments to Unicode - interpreting them as being unequal
if 'multi' in argv[1:3]: # Issue 1008
C:\Python27\lib\site-packages\celery\bin\base.py:398: UnicodeWarning: Unicode equal comparison failed to convert both ar
guments to Unicode - interpreting them as being unequal
if '--version' in …Run Code Online (Sandbox Code Playgroud) 我的脚本是从csv文件中读取数据,csv文件可以有多个英语或非英语单词串.
有时候文本文件有垃圾字符串,我想识别那些字符串并跳过这些字符串并处理其他字符串
doc = codecs.open(input_text_file, "rb",'utf_8_sig')
fob = csv.DictReader(doc)
for row, entry in enumerate(f):
if is_valid_unicode_str(row['Name']):
process_futher
def is_valid_unicode_str(value):
try:
function
return True
except UnicodeEncodeError:
return false
Run Code Online (Sandbox Code Playgroud)
csv输入:
"Name"
"袋è¢âdcx€¹Ã¤Â¸Å½Ã¦Å“‹å‹们çâ€ÂµÃ¥ÂÂå•â€"
"??????"
"John Dove"
Run Code Online (Sandbox Code Playgroud)
我想玷污函数is_valid_unicode_str(),它将识别垃圾字符串并仅处理有效字符串.
我尝试使用解码但是在解码垃圾字符串时它并没有失败
value.decode('utf8')
Run Code Online (Sandbox Code Playgroud)
预期的输出是带有中文和英文字符串的字符串
你能指导我如何实现过滤有效的Unicode文件的功能?
我在CSV文件中有一些俄语数据:
2-????????? ???????? ???', ??? ??????-3, ??????? — ???? ??;??????
2-????????? ???????? ???', ??? ???????, ?????? (?????) — ??????????;??????
2-????????? ???????? ???', ??? ??????-2, ????????? — ?????????;??????
Run Code Online (Sandbox Code Playgroud)
分隔符是;符号.
我想读取数据并将其放入数组中.我尝试使用以下代码读取此数据:
def loadCsv(filename):
lines = csv.reader(open(filename, "rb"),delimiter=";" )
dataset = list(lines)
for i in range(len(dataset)):
dataset[i] = [str(x) for x in dataset[i]]
return dataset
Run Code Online (Sandbox Code Playgroud)
然后我阅读并打印结果:
mydata = loadCsv('krish(csv3).csv')
print mydata
Run Code Online (Sandbox Code Playgroud)
输出:
[['2-\xea\xee\xec\xed\xe0\xf2\xed\xe0\xff \xea\xe2\xe0\xf0\xf2\xe8\xf0\xe0, \xec\xea\xf0 \xd2\xe0\xf1\xf2\xe0\xea-3, \xc0\xed\xee\xf1\xee\xe2\xe0 \x97 \xd2\xee\xeb\xe5 \xe1\xe8', '\xc0\xeb\xec\xe0\xf2\xfb'], ['2-\xea\xee\xec\xed\xe0\xf2\xed\xe0\xff \xea\xe2\xe0\xf0\xf2\xe8\xf0\xe0, \xec\xea\xf0 \xd2\xe0\xf3\xe3\xf3\xeb\xfc, \xc4\xf3\xeb\xe0\xf2\xe8 (\xcd\xe0\xe2\xee\xe8) \x97 \xd2\xee\xea\xf2\xe0\xe1\xe0\xe5\xe2\xe0', '\xc0\xeb\xec\xe0\xf2\xfb'], ['2-\xea\xee\xec\xed\xe0\xf2\xed\xe0\xff …Run Code Online (Sandbox Code Playgroud) 我遇到了这个奇怪的问题,当我跑步时
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/home/fqiao/development/MNIST_data/', one_hot=True)
Run Code Online (Sandbox Code Playgroud)
我得到:
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 199, in read_data_sets
train_images = extract_images(local_file)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 58, in extract_images
magic = _read32(bytestream)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 51, in _read32
return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
File "/usr/lib/python3.5/gzip.py", line 274, in read
return self._buffer.read(size)
File "/usr/lib/python3.5/_compression.py", line 68, in readinto
data = self.read(len(byte_view))
File "/usr/lib/python3.5/gzip.py", line 461, in read
if not self._read_gzip_header():
File "/usr/lib/python3.5/gzip.py", line 404, in _read_gzip_header
magic = self._fp.read(2)
File "/usr/lib/python3.5/gzip.py", …Run Code Online (Sandbox Code Playgroud) 在编写Python 3代码时,我在变量名中使用Unicode文字使我很高兴。今天我有一个奇怪的错误,原来是由于Python无法区分变量?和?,如下面的短代码所示:
? = 'hello'
? = 'goodbye'
print(?) # Prints 'goodbye'
Run Code Online (Sandbox Code Playgroud)
这是错误还是功能?如果是后者,我如何/在哪里可以找到以这种方式一起属于所有这些字符的集合?
在?和?在字符串中使用时,不存在这种区别:
a = '?'
b = '?'
print(a == b) # Prints False
Run Code Online (Sandbox Code Playgroud)
这使我确信这不是我的编辑器/终端的编码问题。
我们还可以使用以下unicodedata模块来确认Python完全了解我们正在处理的字符:
import unicodedata
print(unicodedata.name('?')) # Prints 'GREEK SMALL LETTER RHO'
print(unicodedata.name('?')) # Prints 'GREEK RHO SYMBOL'
Run Code Online (Sandbox Code Playgroud)
我在?(GREEK SMALL LETTER PHI)和?(GREEK PHI SYMBOL)之间发现了相同的行为。
我使用Windows命令行程序(samtools.exe)生成了一个巨大的(6G)txt文件:
.\samtools.exe mpileup -O bamfile.bam > txtfile.tsv
生成的文件实际上是由制表符分隔的表.当我尝试使用pandas.read_table打开它时,它给了我:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
当我试图打印文件的第一行时,它是这样的:
ÿþAL645882 473 N 1 ^!c I 1
除了第一个字符外,一切正常.如果我读它使用'rb',确实第一个字符是0xff.
我真的希望这个表被读作一个pandas DataFrame,文件很大,反正我还能让python忽略这个0xff字节吗?或者只是删除文件中的字节?
提前致谢!
我正在尝试编写一个可以运行python 2和3的程序.它从网站读取字符并写入文件.我已经unicode_literals从__future__ 导入了.
直接尝试写一个看起来像这样的字符串:
txt = u'his$\u2026\n'
Run Code Online (Sandbox Code Playgroud)
会导致UnicodeEncodeError:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 4: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
将它写入python2中的文件的唯一方法是:
fp = open("/tmp/test", "w")
txt2 = txt.encode('utf-8')
fp.write(txt2) # It works
type(txt2) # str - that is why it works
Run Code Online (Sandbox Code Playgroud)
但是,尝试在python3中重用相同的代码是不行的,因为在python 3中,
type(txt2) # is byte type
Run Code Online (Sandbox Code Playgroud)
例如
txt.encode('utf-8')
b'his$\xe2\x80\xa6\n'
Run Code Online (Sandbox Code Playgroud)
强制a fp.write(txt2)会抛出TypeError:
TypeError: write() argument must be str, not bytes
Run Code Online (Sandbox Code Playgroud)
因此,可以txt = u'his$\u2026\n'在python 2和3中使用相同的代码块写入文件中.(除了在fp.write上使用包装器)
python ×10
python-unicode ×10
python-3.x ×2
unicode ×2
celery ×1
csv ×1
django ×1
encoding ×1
file-io ×1
mojibake ×1
pandas ×1
python-2.7 ×1
python-2.x ×1
python-2to3 ×1
regex ×1
tensorflow ×1
traceback ×1
wordnet ×1
xml ×1