至少有两种方法可以在python中写入文件:
f = open(file, 'w')
f.write(string)
Run Code Online (Sandbox Code Playgroud)
要么
f = open(file, 'w')
print >> f, string # in python 2
print(string, file=f) # in python 3
Run Code Online (Sandbox Code Playgroud)
这两者有区别吗?或者是更多的Pythonic?我正在尝试写一堆HTML文件,所以我需要通过我的文件写一堆写/打印语句(但我不需要模板引擎).
这是一个有点奇怪的请求,但我正在寻找一种方法将列表写入文件,然后在其他时间读回.
我没有办法重新制作列表,以便正确形成/格式化它们,如下例所示.
我的列表包含以下数据:
test
data
here
this
is one
group :)
test
data
here
this
is another
group :)
Run Code Online (Sandbox Code Playgroud) 在使用round()函数时,我注意到我得到了两个不同的结果,具体取决于我是否没有明确选择要包含的小数位数或选择数字为0.
x = 4.1
print(round(x))
print(round(x, 0))
Run Code Online (Sandbox Code Playgroud)
它打印以下内容:
4
4.0
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我有这样的代码(简化):
def outer():
ctr = 0
def inner():
ctr += 1
inner()
Run Code Online (Sandbox Code Playgroud)
但是ctr会导致错误:
Traceback (most recent call last):
File "foo.py", line 9, in <module>
outer()
File "foo.py", line 7, in outer
inner()
File "foo.py", line 5, in inner
ctr += 1
UnboundLocalError: local variable 'ctr' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?我认为嵌套的范围可以让我这样做.我试过'全球',但它仍然无效.
是否有理由进行list.append评估?或者只是在成功发挥作用时返回0的C约定?
>>> u = []
>>> not u.append(6)
True
Run Code Online (Sandbox Code Playgroud) 给定一个包含字符串的变量是否可以快速将其转换为另一个原始字符串变量?
以下代码应说明我所追求的内容:
line1 = "hurr..\n..durr"
line2 = r"hurr..\n..durr"
print(line1 == line2) # outputs False
print(("%r"%line1)[1:-1] == line2) # outputs True
Run Code Online (Sandbox Code Playgroud)
到目前为止我找到的最接近的是%r格式化标志,它似乎返回一个原始字符串,尽管在单引号内.有没有更简单的方法来做这种事情?
我有这段代码:
numbers = range(1, 50)
for i in numbers:
if i < 20:
numbers.remove(i)
print(numbers)
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是:
[2,4,6,8,10,12,14,16,18,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35 ,36,37,38,39,40,41,42,43,44,45,46,47,48,49]
当然我希望结果中没有出现20以下的数字,我假设我在删除时做错了.
我有一些奇怪的问题PIL没有调整图像大小.
from PIL import Image
img = Image.open('foo.jpg')
width, height = img.size
ratio = floor(height / width)
newheight = ratio * 150
img.resize((150, newheight), Image.ANTIALIAS)
img.save('mugshotv2.jpg', format='JPEG')
Run Code Online (Sandbox Code Playgroud)
此代码运行时没有任何错误,并生成mugshotv2.jpg在正确的文件夹中命名的图像,但它不会调整它的大小.它做了一些事情,因为图片的大小从120 kb下降到20 kb,但尺寸保持不变.
也许你也可以建议用更少的代码将图像裁剪成正方形.我有点想到这样Image.thumbnail做,但它做的是它将我的图像按宽度缩放到150像素,高度为100像素.
简单的问题,但由于我是python的新手,从php转发,我得到了一些错误.
我有以下简单的类:
User(object)
fullName = "John Doe"
user = User()
Run Code Online (Sandbox Code Playgroud)
在PHP中,我可以执行以下操作:
$param = 'fullName';
echo $user->$param; // return John Doe
Run Code Online (Sandbox Code Playgroud)
我怎么在python中这样做?
我想使用Python将所有文本文件从一个文件夹移动到另一个文件夹.我找到了这段代码:
import os, shutil, glob
dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version\ 4/Blobs '
try:
os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
# The directory already existed, nothing to do
pass
for txt_file in glob.iglob('*.txt'):
shutil.copy2(txt_file, dst)
Run Code Online (Sandbox Code Playgroud)
我希望它移动文件Blob夹中的所有文件.我没有收到错误,但它也没有移动文件.