Tag: python

NameError:全局名称“unicode”未定义 – 在Python 3中

我正在尝试使用名为bidi的Python包。 在这个包(algorithm.py)的一个模块中,有一些行给我错误,虽然它是包的一部分。 这里是行: # utf-8 ? we need unicode if isinstance(unicode_or_str, unicode): text = unicode_or_str decoded = False else: text = unicode_or_str.decode(encoding) decoded = True 这里是错误信息: Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> bidi_text = get_display(reshaped_text) File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py", line 602, in get_display if isinstance(unicode_or_str, unicode): NameError: global name 'unicode' is not […]

Tuple解包顺序更改分配的值

我认为两者是相同的。 nums = [1, 2, 0] nums[nums[0]], nums[0] = nums[0], nums[nums[0]] print nums # [2, 1, 0] nums = [1, 2, 0] nums[0], nums[nums[0]] = nums[nums[0]], nums[0] print nums # [2, 2, 1] 但结果是不同的。 为什么结果不同? (为什么是第二个结果?)

Python – 数据框的维数

Python新手 在R中,可以使用dim(…)来获得matrix的维数。 Python Pandas中的数据框相应的function是什么?

是否有一个相当于C ++的“for … else”Python循环?

Python有一个有趣for陈述,可以让你指定一个else子句。 在这样一个构造中: for i in foo: if bar(i): break else: baz() else子句在for之后执行,但是只有在for正常终止(而不是break )的情况下。 我想知道是否有一个相当于C + +? 我可以使用for … else ?

CSV阅读器(Python)中“行包含NULL字节”

我试图编写一个查看.CSV文件(input.csv)的程序,并只重写以文本文件(output.txt)中列出的某个元素(corrected.csv)开始的行。 这是我的程序现在看起来像: import csv lines = [] with open('output.txt','r') as f: for line in f.readlines(): lines.append(line[:-1]) with open('corrected.csv','w') as correct: writer = csv.writer(correct, dialect = 'excel') with open('input.csv', 'r') as mycsv: reader = csv.reader(mycsv) for row in reader: if row[0] not in lines: writer.writerow(row) 不幸的是,我不断收到这个错误,我不知道这是什么。 Traceback (most recent call last): File "C:\Python32\Sample Program\csvParser.py", line 12, […]

为什么Python计算“hashlib.sha1”与文件中的“git hash-object”不同?

我正在尝试计算文件的SHA-1值。 我编造了这个脚本: def hashfile(filepath): sha1 = hashlib.sha1() f = open(filepath, 'rb') try: sha1.update(f.read()) finally: f.close() return sha1.hexdigest() 对于一个特定的文件,我得到这个哈希值: 8c3e109ff260f7b11087974ef7bcdbdc69a0a3b9 但是,当我用git hash_object计算值,然后我得到这个值: d339346ca154f6ed9e92205c3c5c38112e761eb7 他们怎么不一样? 我做错了什么,或者我可以忽略它们的区别?

在Django的pipe理界面中,是否有复制项目的方法?

只是想知道是否有一个简单的方法来添加function来复制pipe理界面中的现有列表? 在数据input中,我们遇到了很多项目与其他项目共享通用数据的情况,为了节省时间,快速复制现有列表并仅更改已更改的数据将是非常好的。 使用更好的模型结构将是减less数据重复的一种方法,但是在将来可能会出现重复数据需要个别更改的情况。

Python – calendar.timegm()与time.mktime()

我似乎很难得到这个头。 calendar.timegm()和time.mktime()什么time.mktime() ? 说我有一个datetime.datetime没有tzinfo附加,不应该两个给出相同的输出? 难道他们都不是把时代和date之间的秒数作为参数吗? 而由于通过的date没有tzinfo,是不是秒数相同? >>> import calendar >>> import time >>> import datetime >>> d = datetime.datetime(2010, 10, 10) >>> calendar.timegm(d.timetuple()) 1286668800 >>> time.mktime(d.timetuple()) 1286640000.0 >>>

testing一个类是否从另一个类inheritance

这个问题比Django相关的Python更多。 我想testing写这个函数的testing,我正在使用我设置的字段dynamic获取Django表单。 def quiz_form_factory(question): properties = { 'question': forms.IntegerField(widget=forms.HiddenInput, initial=question.id), 'answers': forms.ModelChoiceField(queryset=question.answers_set) } return type('QuizForm', (forms.Form,), properties) 我想testing一下,QuizForm类返回是从forms.Forminheritance的。 就像是: self.assertTrue(QuizForm isinheritedfrom forms.Form) # I know this does not exist 有没有办法做到这一点?

其他目的,最后在exception处理

else和finally的exception处理部分是多余的? 例如,以下两个代码片段之间是否有区别? try: foo = open("foo.txt") except IOError: print("error") else: print(foo.read()) finally: print("finished") 和 try: foo = open("foo.txt") print(foo.read()) except IOError: print("error") print("finished") 更一般地说, else的内容不能总是被移入try ,而finally的内容不能被移出try / catch块? 如果是的话, else的目的是else ? 这只是为了提高可读性吗?