我遇到了一个奇怪的问题,无论我使用什么编码,都会返回相同的错误。代码运行良好,没有Python2.7.8中的编码部分,但它在2.7.6中中断,这是我在所有开发中使用的版本。
import MIDI_PY2 as md
import glob
import ast
import os
dir = '/Users/user/Desktop/sample midis/'
os.chdir(dir)
file_list = []
for file in glob.glob('*.mid'):
file_list.append((dir + file))
di
遵循安装的说明(包括显式安装spatialindex)之后,使用
brew install spatialindex
pip install osmnx
运行的第一个基本示例
import osmnx as ox
G = ox.graph_from_place('Manhattan Island, New York City, New York, USA', network_type='drive')
ox.plot_graph(ox.project_graph(G))
在项目的中,我得到了
Traceback (most recent call last):
我试图破解密码,但无法成功。这是密码。
from hashlib import sha1 as sha_constructor
import random
def generate_sha1(string, salt=None):
if not isinstance(string, (str, str)):
string = str(string)
if isinstance(string, str):
string = string.encode("utf-8")
if not salt:
salt
当我试图运行这个程序时,我得到了以下错误
我将array导入为arr
import array as arr
x = arr.array([3, 6, 9, 12])
x/3.0
print(x)
我希望输出是
array([1, 2, 3, 4])
错误:
x = arr.array([3, 6, 9, 12])
TypeError: array() argument 1 must be a unicode character, not list
有人能解释一下为什么这事不会发生吗?
我正在编写的代码应该是Python2.7和Python兼容的。当试图使用Python2.7运行我的代码时,我会遇到以下问题。
我在每个文件中从unicode_literals中导入__future__,并且很难使array函数正常工作。
from array import array
from __future__ import unicode_literals
试图使字符数组无效
array("c", "test")
> TypeError: must be char, not unicode
试图使unicode数组也不起作用
array("u
检查var是否有str、unicode或空,但没有其他任何内容,什么是最好的呢?
我试过
if not isinstance(ads['number'], (str, unicode, None)):
....
但在例外情况下:
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
`
使用此查询:
04/25/2017 00:42:28.180 INFO (u"UPDATE posts SET translated_text='Unroll.me CEO dice que es "desgarrador" que los usuarios se molesten Unroll.me vendi\xf3 sus datos de correo electr\xf3nico anonimizado a Uber', detected_language='en' WHERE post_id=2",
我已经使用模块3to2将我的python3代码转换为python2,现在无法写入CSV文件。
转换代码后的TypeError: must be unicode, not str
def writeCSV(self,filepath,data):
with open(filepath, u'a', newline=u'') as file:
writer = csv.writer(file)
writer.writerow(data)
传递的数据是'10/06/2020 04:28:57 A
使用python2.7.3,以下内容不会引发TypeError。
>>> unicode(u'')
u''
但是,传递errors参数会引发TypeError: decoding Unicode is not supported
>>> unicode(u'',errors='replace')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Type
在游戏中,我试图打印文本'10×10‘。10作为整数存储在sizeOfGrid变量中。我目前的代码是:
text = font.render((str(sizeOfGrid),'x',str(sizeOfGrid)), 1, (10, 10, 10))
textpos = (32,210)
screenDisplay.blit
当我运行程序时,它返回错误exceptions.TypeError: text must be string or unicode。我不明白这个错误,也不明白是怎么回事,因为我已经把10转换成一个字符串。
我希望这个方法是完全可以理解的,只看代码和注释。
def add_error(error_dict, key, err):
"""Given an error message, or a list of error messages, this method
adds it/them to a dictionary of errors.
Doctests:
>>> add_error({}, 'key1', 'error1')
{'key1':
我想写一些字符串到文件,这不是英语,他们是在阿塞拜疆语言。即使我执行utf-8编码,我也会得到以下错误:
TypeError: write() argument must be str, not bytes
即使我把代码写成:
t_w = text_list[y].encode('utf-8')
new_file.write(t_w)
new_file.write('\n')
我得到以下错误:
TypeError: write() argument must be str, not bytes
我不以
使用这个问题:作为模型,我编写了以下代码来生成csv文件:
df.to_csv('/Users/Lab/Desktop/filteredwithheading.txt', sep='\s+', header=True)
但它返回以下错误:
TypeError: "delimiter" must be an 1-character string
我已经在这里查找了的文档,但是我不知道我遗漏了什么,或者这个错误意味着什么。我还尝试在代码中使用(sep='\s'),但得到了相同的错误。
我有以下代码
import nltk, os, json, csv, string, cPickle
from scipy.stats import scoreatpercentile
lmtzr = nltk.stem.wordnet.WordNetLemmatizer()
def sanitize(wordList):
answer = [word.translate(None, string.punctuation) for word in wordList]
answer = [lmtzr.lemmatize(word.lower()) for word in answer]
在this location上使用python2.7中的s2sphere库时,我得到了以下错误。 ljust() argument 2 must be char, not unicode 方法是link to the file on GH @classmethod
def from_token(cls, token):
"""Creates a CellId from a hex encoded cell id string, called a token.
:param str token:
A
我目前在从服务器获取哈希时遇到问题。
TypeError: Unicode-objects must be encoded before hashing
下面是错误:
data = urllib2.urlopen(url) #request
hash = hashlib.md5(data.read().decode('utf-8')).hexdigest() #
实际上,我试图将decode()替换为encode(),但它给出了另一个错误:
AttributeError: 'bytes' object has no attribute 'encode&
我试图运行以下Python代码:
with io.open(outfile, 'w' ) as processed_text, io.open(infile, 'r') as fin:
for line in fin:
processed_text.write(preprocess(line.rstrip())+'\n')
但得到了TypeError: must be unicode, not str
我该如何解决这个问题?我在这里搜索了类似的问题,并找到了一个
with io.open(outfile, 'w
突然,我收到一个错误,说TypeError: on_delete must be callable.,我不知道如何解决这个错误,因为我的代码中没有提到field=models.ForeignKey(default=1, on_delete='CASCADE', to='main.Category'),。
File "/home/arch/myproject/main/migrations/0014_auto_20191025_1154.py", line 6, in <module>
class Migration(mig
我有一个用Python3 (使用http.server中的类)编写的简单web服务器,我正在将它从2移植到3。
我有以下代码:
# More code here...
postvars = cgi.parse_qs(self.rfile.read(length),
keep_blank_values=1)
something.json_run(json.dumps(postvars))
这会抛出:
TypeError: keys must be a string
通过检查数据,我确定parse_qs似乎将键编码为字节,这就是抛出错误的原因(显然,
在我的Win xp机器上,我得到了一个简单的脚本,它在Python 2.7.1下工作得很好。现在有了一台安装了python 3.1.3的win7机器。
代码是:
owriter.writerow(dtime[1][1])
dtime[1][1]=['30-Aug-10 16:00:00', '2.5', '15']
收到此错误消息:TypeError: must be bytes or buffer, not str
我应该做哪些更改?
谢谢。
我正在尝试让一个基本的服务器(从开头的Python复制)来发送一个字符串。
错误:
c.send( "XXX" )
TypeError: must be bytes or buffer, not str
它似乎在酸洗对象时起作用。我找到的所有例子,似乎都能发送一个字符串没有问题。
任何帮助都会很感激,
斯蒂芬
import socket
import pickle
s = socket.socket()
host = socket.gethostname()
port = 80
s.bind((host, port))
s.listen(5)
while T
目前正在学习try和except,并尝试通过除以数字来捕获错误 这是我的代码: def divide(a, b):
try:
return a/b
except ZeroDivisionError:
return "dividing by zero not possible "
except TypeError:
return "a and b must both be integers"
except NameError:
return "a and
我试图在python代码中将从日期选择器中选择的日期返回到我的sql查询中。我还尝试使用encode(utf-8)删除unicode字符串,但是仍然会收到错误。
我对蟒蛇很陌生。有人能帮我解决这个问题吗?我正在使用python烧瓶创建网页。
if request.method=='POST':
dateval2 = request.form['datepick']
dateval = dateval2.encode('utf-8')
result = ("SELECT * FROM OE_TAT w
好的,我试着写一个字符串到md5散列代码的基本转换器,但是当我运行我的程序时,我总是得到一个错误,说:
Traceback (most recent call last):
File "C:\Users\Shane\Documents\Amer CISC\lab4.py", line 30, in <module>
assertEqual (computeMD5hash("The quick brown fox jumps over the lazy dog"),("9e107d9d372bb6826bd81d3542a419d6
下午好,我尝试了几天,从在Python3下运行的lincolnloop获得。它在Python2.x上运行得很好。-
总的来说,我对Python编程非常陌生,但我认为到目前为止我已经完成了我的家庭作业。
第一个错误:
File "/usr/lib/python3.2/qrcode/util.py", line 274, in __init__
if not isinstance(data, basestring):
NameError: global name 'basestring' is not defined
所以基弦不再存在于Python3中
以下代码来自“Python for Data Analysis”:
from io import StringIO
import io
tag = '<a href="http://www.google.com">Google</a>'
root = objectify.parse(io.StringIO(tag).getroot())
执行代码会产生以下错误:
TypeError: initial_value must be unicode or None, not str
请帮帮我!
我正在尝试将Pandas数据帧写入csv,并使用‘trying’作为分隔符。
data.to_csv(file_path, sep = '€')
然而,我得到了一个错误:
TypeError: "delimiter" must be an 1-character string
我如何克服这个问题?我必须使用欧元符号作为分隔符