Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。


10
从Python源代码生成UML图的最佳方法是什么?[关闭]
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案会得到事实,参考或专业知识的支持,但是这个问题可能会引起辩论,争论,民意测验或进一步的讨论。如果您认为此问题可以解决并且可以重新提出,请访问帮助中心以获取指导。 7年前关闭。 一位同事正在寻找从Python源代码堆中生成UML类图的方法。他主要对继承关系感兴趣,对组合关系也颇有兴趣,并且不太在乎仅仅是Python原语的类属性。 源代码非常简单明了,没有太大的弊端-例如,它没有做任何花哨的元类魔术。(这主要是从Python 1.5.2时代开始的,其中散布着一些“现代”的2.3ish东西。) 有什么最好的现有解决方案推荐?
258 python  uml  diagram 

19
有没有一种方法可以分离matplotlib图,以便继续计算?
在Python解释器中执行了这些指令后,将获得一个带有绘图的窗口: from matplotlib.pyplot import * plot([1,2,3]) show() # other code 不幸的是,show()当程序进行进一步的计算时,我不知道如何继续交互式地探索创建的图形。 有可能吗?有时计算很长,如果可以在检查中间结果时进行计算,则将有所帮助。
258 python  matplotlib  plot 

21
如何在TensorFlow中打印Tensor对象的值?
我一直在使用TensorFlow中矩阵乘法的入门示例。 matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1, matrix2) 当我打印产品时,它会将其显示为Tensor对象: <tensorflow.python.framework.ops.Tensor object at 0x10470fcd0> 但是我怎么知道的价值product呢? 以下内容无济于事: print product Tensor("MatMul:0", shape=TensorShape([Dimension(1), Dimension(1)]), dtype=float32) 我知道图形可以继续运行Sessions,但是没有任何方法可以在Tensor不运行图形的情况下检查对象的输出session吗?

7
如何删除列表中的项目(如果存在)?
我new_tag从一个表单文本字段self.response.get("new_tag")和selected_tags复选框字段 self.response.get_all("selected_tags") 我将它们像这样组合: tag_string = new_tag new_tag_list = f1.striplist(tag_string.split(",") + selected_tags) (f1.striplist此函数会在列表中的字符串内去除空格。) 但在这种情况下tag_list是空的(没有新的标签进入),但也有一些selected_tags,new_tag_list包含一个空字符串" "。 例如,来自logging.info: new_tag selected_tags[u'Hello', u'Cool', u'Glam'] new_tag_list[u'', u'Hello', u'Cool', u'Glam'] 我如何摆脱空字符串? 如果列表中有一个空字符串: >>> s = [u'', u'Hello', u'Cool', u'Glam'] >>> i = s.index("") >>> del s[i] >>> s [u'Hello', u'Cool', u'Glam'] 但是,如果没有空字符串: >>> s = [u'Hello', u'Cool', …
258 python  list 

6
为什么我需要用b来用Base64编码字符串?
在此python示例之后,我使用以下代码将字符串编码为Base64: >>> import base64 >>> encoded = base64.b64encode(b'data to be encoded') >>> encoded b'ZGF0YSB0byBiZSBlbmNvZGVk' 但是,如果我忽略了领导b: >>> encoded = base64.b64encode('data to be encoded') 我收到以下错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python32\lib\base64.py", line 56, in b64encode raise TypeError("expected bytes, not %s" % s.__class__.__name__) TypeError: expected bytes, …

11
Flask可以有可选的URL参数吗?
是否可以直接声明Flask URL可选参数? 目前,我正在按照以下方式进行: @user.route('/<userId>') @user.route('/<userId>/<username>') def show(userId, username=None): pass 我如何直接说这username是可选的?
258 python  flask 

14
JSONDecodeError:预期值:第1行第1列(字符0)
Expecting value: line 1 column 1 (char 0)尝试解码JSON 时出现错误。 我用于API调用的URL在浏览器中可以正常工作,但是通过curl请求完成时会出现此错误。以下是我用于curl请求的代码。 错误发生在 return simplejson.loads(response_json) response_json = self.web_fetch(url) response_json = response_json.decode('utf-8') return json.loads(response_json) def web_fetch(self, url): buffer = StringIO() curl = pycurl.Curl() curl.setopt(curl.URL, url) curl.setopt(curl.TIMEOUT, self.timeout) curl.setopt(curl.WRITEFUNCTION, buffer.write) curl.perform() curl.close() response = buffer.getvalue().strip() return response 完整回溯: 追溯: File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. …
258 python  json  api  curl 

12
将Django模型对象转换为所有字段均完整的dict
如何将Django模型对象转换为具有所有字段的字典?理想情况下,所有内容都包含带有的外键和字段editable=False。 让我详细说明。假设我有一个类似以下的Django模型: from django.db import models class OtherModel(models.Model): pass class SomeModel(models.Model): normal_value = models.IntegerField() readonly_value = models.IntegerField(editable=False) auto_now_add = models.DateTimeField(auto_now_add=True) foreign_key = models.ForeignKey(OtherModel, related_name="ref1") many_to_many = models.ManyToManyField(OtherModel, related_name="ref2") 在终端中,我已执行以下操作: other_model = OtherModel() other_model.save() instance = SomeModel() instance.normal_value = 1 instance.readonly_value = 2 instance.foreign_key = other_model instance.save() instance.many_to_many.add(other_model) instance.save() 我想将其转换为以下字典: {'auto_now_add': …

11
如何避免“ RuntimeError:字典在迭代过程中更改大小”错误?
我检查了所有其他问题,并发现了相同的错误,但没有找到有帮助的解决方案= / 我有一个列表字典: d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]} 其中某些值为空。在创建这些列表的最后,我想在返回字典之前删除这些空列表。当前,我正在尝试执行以下操作: for i in d: if not d[i]: d.pop(i) 但是,这给了我运行时错误。我知道您无法在字典中进行迭代时添加/删除字典中的元素...那么解决这个问题的方法是什么?
257 python  list  dictionary  loops 

11
使用登录多个模块
我有一个具有以下结构的小型python项目- Project -- pkg01 -- test01.py -- pkg02 -- test02.py -- logging.conf 我计划使用默认的日志记录模块将消息打印到stdout和日志文件。要使用日志记录模块,需要进行一些初始化- import logging.config logging.config.fileConfig('logging.conf') logger = logging.getLogger('pyApp') logger.info('testing') 目前,在开始记录消息之前,我会在每个模块中执行此初始化。是否可以只在一个地方执行一次初始化,以便通过记录整个项目来重复使用相同的设置?
257 python  logging  config 


9
UnicodeDecodeError,无效的继续字节
为什么以下项目失败?为什么使用“ latin-1”编解码器成功? o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving v = o.decode("utf-8") 结果是: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' …
257 python  unicode  decode 

11
如何从熊猫数据框中删除行列表?
我有一个数据框df: >>> df sales discount net_sales cogs STK_ID RPT_Date 600141 20060331 2.709 NaN 2.709 2.245 20060630 6.590 NaN 6.590 5.291 20060930 10.103 NaN 10.103 7.981 20061231 15.915 NaN 15.915 12.686 20070331 3.196 NaN 3.196 2.710 20070630 7.907 NaN 7.907 6.459 然后,我想删除具有列表中指示的某些序列号的行,假设此时留在这里[1,2,4],: sales discount net_sales cogs STK_ID RPT_Date 600141 20060331 2.709 …
257 python  pandas 

16
将数字范围转换为另一个范围,保持比例
我正在尝试将一个数字范围转换为另一个数字范围,并保持比率。数学不是我的强项。 我有一个图像文件,其中点值的范围可能在-16000.00到16000.00之间,尽管典型范围可能会小得多。我想做的是将这些值压缩到0-100的整数范围内,其中0是最小点的值,而100是最大点的值。即使丢失了一些精度,它们之间的所有点都应保持相对比率,我想在python中做到这一点,但即使是通用算法也足够。我更喜欢可以调整最小/最大或任意一个范围的算法(即,第二个范围可以是-50到800,而不是0到100)。
256 python  math 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.