在Unix shell中我可以这样做来清空一个文件:
cd /the/file/directory/
:> thefile.ext
Run Code Online (Sandbox Code Playgroud)
我将如何在Python中执行此操作?
是这样os.system的方式,我不知道如何,因为我必须在彼此之后发送2个动作,即cd然后是:>.
如何将Python Enum成员序列化为JSON,以便将生成的JSON反序列化为Python对象?
例如,这段代码:
from enum import Enum
import json
class Status(Enum):
success = 0
json.dumps(Status.success)
Run Code Online (Sandbox Code Playgroud)
导致错误:
TypeError: <Status.success: 0> is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这种情况?
如果我尝试执行以下操作:
things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)
我在Python 3.x中收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
......以及Python 2.x中的类似错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我在Project Euler(顺便说一句很棒的网站)上遇到了第10个问题的奇怪经历.任务是计算低于200万的所有素数之和.
我使用了一个int作为总和,我的算法产生了答案,但是当我粘贴它来验证答案时,它是错误的.
结果是结果太大而不适合int,但这不会导致溢出错误或其他什么?相反,它只返回了一个远离真实答案的值.
当我把类型改为long时,一切都很笨拙.
我试图复制(如果可能改善)的Python 2.x的在3.x的排序行为,使双方可订购的类型,如int,float等进行排序预期,并且互相unorderable类型的输出中进行分组.
这是我正在谈论的一个例子:
>>> sorted([0, 'one', 2.3, 'four', -5]) # Python 2.x
[-5, 0, 2.3, 'four', 'one']
Run Code Online (Sandbox Code Playgroud)
>>> sorted([0, 'one', 2.3, 'four', -5]) # Python 3.x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()
Run Code Online (Sandbox Code Playgroud)
我之前尝试过这个,使用一个关键参数的类sorted()(参见
为什么这个用于排序异构序列的关键类表现奇怪?)从根本上被打破了,因为它的方法是
如BrenBarn的出色答案所解释的那样,可以导致不及物处理.
我最初在没有尝试编码的情况下拒绝的一种天真的方法是使用返回(type, value)元组的键函数:
def motley(value):
return repr(type(value)), value
Run Code Online (Sandbox Code Playgroud)
但是,这不符合我的要求.首先,它打破了相互可订购类型的自然顺序:
>>> sorted([0, 123.4, 5, -6, 7.89])
[-6, 0, 5, 7.89, 123.4]
>>> sorted([0, …Run Code Online (Sandbox Code Playgroud) 考虑这种情况:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
walk = os.walk('/home')
for root, dirs, files in walk:
for pathname in dirs+files:
print os.path.join(root, pathname)
for root, dirs, files in walk:
for pathname in dirs+files:
print os.path.join(root, pathname)
我知道这个例子有点多余,但您应该考虑我们需要walk多次使用相同的数据.我有一个基准测试场景,必须使用相同的walk数据才能获得有用的结果.
我试图walk2 = walk在第二次迭代中克隆并使用,但它没有用.问题是......我怎么能复制它?它有可能吗?
先感谢您.
Python 3.4引入了一个新模块enum,它为该语言添加了枚举类型.该文档enum.Enum提供了一个示例来演示如何扩展它:
>>> class Planet(Enum):
... MERCURY = (3.303e+23, 2.4397e6)
... VENUS = (4.869e+24, 6.0518e6)
... EARTH = (5.976e+24, 6.37814e6)
... MARS = (6.421e+23, 3.3972e6)
... JUPITER = (1.9e+27, 7.1492e7)
... SATURN = (5.688e+26, 6.0268e7)
... URANUS = (8.686e+25, 2.5559e7)
... NEPTUNE = (1.024e+26, 2.4746e7)
... def __init__(self, mass, radius):
... self.mass = mass # in kilograms
... self.radius = radius # in meters
... @property
... def surface_gravity(self):
... # universal …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些日期/时间转换为UTC,我认为这在Python中很简单 - 包括电池,对吗?好吧,除了Python(2.6)不包含任何tzinfo类之外,它很简单.没问题,快速搜索会出现python-dateutil,它应该完全符合我的需要.
问题是我需要在Windows上安装它.我能够使用7-zip升级.tar.gz2发行版,但现在我留下了一系列文件,没有关于如何继续的指导.当我尝试运行setup.py时,我收到错误"没有名为setuptools的模块".
请参阅以下代码:
import datetime
import pytz
fmt = '%Y-%m-%d %H:%M:%S %Z'
d = datetime.datetime.now(pytz.timezone("America/New_York"))
d_string = d.strftime(fmt)
d2 = datetime.datetime.strptime(d_string, fmt)
print d_string
print d2.strftime(fmt)
Run Code Online (Sandbox Code Playgroud)
输出是
2013-02-07 17:42:31 EST
2013-02-07 17:42:31
Run Code Online (Sandbox Code Playgroud)
时区信息在翻译中丢失了.
如果我将'%Z'切换为'%z',我明白了
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用python-dateutil,但我发现它很棒,我无法在datetime实现这个简单的功能,并且必须引入更多的依赖?
我想将一个函数应用于dict中的所有值,并将其存储在单独的dict中.我只是想看看我如何使用python并想看看如何重写这样的东西
for i in d:
d2[i] = f(d[i])
Run Code Online (Sandbox Code Playgroud)
喜欢的东西
d2[i] = f(d[i]) for i in d
Run Code Online (Sandbox Code Playgroud)
编写它的第一种方式当然很好,但我试图弄清楚如何改变python语法