相关疑难解决方法(0)

在python中为什么它不会没有换行打印

我是学习python的新手.我不明白为什么print命令会在屏幕上输出所有变量,但是写入命令只能写入2个前两个变量.

print "Opening the file..."
target = open(filename, 'a+')

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
line4 = line1 + "\n" + line2 + "\n" + line3

# This command prints all 3 (line1,line2,line3) variables on terminal
print line4

#This command only writes line1 and line2 variables in file
target.write(line4)

print "close the file"
target.close()
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
191
查看次数

close()Python中的变量

我有以下代码:

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()
Run Code Online (Sandbox Code Playgroud)

而在我的书的练习,它说一个"有你的脚本也做了close()txttxt_again变量.当你与他们所做的要关闭文件是很重要的.

如何关闭变量?

python file

2
推荐指数
1
解决办法
2881
查看次数

使用pickle加载课程状态

我试图用咸菜弄湿我的脚,所以我写了一些这样的示例代码:

class start(tk.Frame):
    def __init__(self,*args,**kwargs):
        tk.Frame.__init__(self,*args,**kwargs)
        frame = tk.Frame(self,width=600,height=600)
        self.val = 0
        self.plusButton = tk.Button(self,text="plus",command=self.plus)
        self.plusButton.pack()
        self.valLabel = tk.Label(self)
        self.valLabel.pack()
        self.saveButton = tk.Button(self,text="save",command=self.save)
        self.saveButton.pack()
        self.loadButton = tk.Button(self,text="load",command=self.load)
        self.loadButton.pack()
    def load(self):
        self.__dict__ = pickle.load(open( "testtesttest.p", "rb" ))
    def plus(self):
        self.val += 1 
        self.valLabel.config(text="%d"%(self.val))
    def save(self):
        pickle.dump(self.__getstate__, open( "testtesttest.p", "wb" ))

    def __getstate__(self):
        return self.__getstate__


if __name__=='__main__':
   root = tk.Tk()

   start(root).pack()
   root.mainloop()
Run Code Online (Sandbox Code Playgroud)

因此,此应用程序的目标是一旦我按下加号按钮,屏幕上的数字就会越来越多。如果我保存它,请关闭窗口,重新打开它,然后单击“加载”按钮,我将看到上次增加该数字的时间。我对pickle还是很陌生,当前代码将其反馈给我:

    Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__return self.func(*args)
File "/Users/caoanjie/pickleDemotry.py", line …
Run Code Online (Sandbox Code Playgroud)

python tkinter pickle

1
推荐指数
1
解决办法
1259
查看次数

使用 astropy 从旧数据写入新的 FITS 文件

我对 FITS 文件执行了一个非常简单的操作(数据是 numpy 数组格式),但我无法将其保存为新文件或覆盖现有文件。

我正在重写一些使用 numpy pyfits 模块处理天文 FITS 文件的旧代码 - 我想更新它以使用 astropy.io fits 模块。具体来说,我使用的一些数据是 3D 的,有些是 4D 的。4D 的东西只是一个约定 - 第 4 轴不包含有用的信息(可以在此处找到数据示例:http : //www.mpia.de/THINGS/Data_files/NGC_628_NA_CUBE_THINGS.FITS)。所以我更喜欢删除额外的轴,然后我的其余代码可以在没有任何特殊要求的情况下继续进行。

这是我使用的基于 pyfits 的旧代码,效果很好:

import numpy
import pyfits

filename = 'NGC628.fits'
outfile  = 'NGC628_reshaped.fits'

# Get the shape of the file
fitsfile=pyfits.open(filename)
image = fitsfile[0].data
header =fitsfile[0].header
z = image.shape[1]  # No. channels                  
y = image.shape[2]  # No. x pixels
x = image.shape[3]  # No. y pixels

newimage = numpy.reshape(image,[z,y,x])

pyfits.core.writeto(outfile,newimage,header, clobber=True) …
Run Code Online (Sandbox Code Playgroud)

numpy python-3.x pyfits astropy

1
推荐指数
1
解决办法
2570
查看次数

使用python context来覆盖变量?

我很确定我已经看过这个,但我无法正确理解语法.我想在测试期间覆盖模块"常量".我可以写这样的代码:

import mymodule

try:
   hold = mymodule.FOO
   mymodule.FOO = 'test value'
   do_something()
finally:
   mymodule.FOO = hold
Run Code Online (Sandbox Code Playgroud)

在我看来应该有一种方法来做一个"with"语句,如:

with mymodule.FOO = 'test value':
    do_something()
Run Code Online (Sandbox Code Playgroud)

我的思想欺骗了我吗?是否有一个简单的语法来做我想要的?

python with-statement

0
推荐指数
1
解决办法
309
查看次数

标签 统计

python ×4

astropy ×1

file ×1

numpy ×1

pickle ×1

pyfits ×1

python-3.x ×1

tkinter ×1

with-statement ×1