通過教程,我已知道你可以在同一個語句定義兩個變量,例如:這兩個變量如何解包?
In [15]: a, b = 'hello', 'hi!'
In [16]: a
Out[16]: 'hello'
In [17]: b
Out[17]: 'hi!'
以及如何應用到這裏?
fh, opened = cbook.to_filehandle(fname, 'w', return_opened = True)
我打了招呼進一步:
In [18]: fh
Out[18]: <open file 'attempt.csv', mode 'w' at 0xaac89d0>
In [19]: opened
Out[19]: True
我的問題與 '開' 來真的。通常情況下,如果定義了兩個變量,就會有逗號,然後無論那裏會定義「已打開」。不是這種情況。即使這個問題迫在眉睫,「打開」等於True,我認爲是因爲'return_opened = True'。那太奇怪了,因爲我不記得在任何教程中,你可以在變量之前添加一個'return_'來影響該變量。
我用它玩多一些,我改變時真亦假,我得到這個:
In [10]: fh, opened = cbook.to_filehandle(fname, 'w', return_opened = False)
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
/home/blahblahblah/Documents/Programming/EXERCISES/piece.py in <module>()
----> 1
2
3
4
5
IOError: [Errno 9] Bad file descriptor
所以我想它只接受假。
我想我想如果有人能解釋這裏發生了什麼。
Gracias amigos!
這裏是整個代碼:
if missingd is None:
missingd = dict()
def with_mask(func):
def newfunc(val, mask, mval):
if mask:
return mval
else:
return func(val)
return newfunc
formatd = get_formatd(r, formatd)
funcs = []
for i, name in enumerate(r.dtype.names):
funcs.append(with_mask(csvformat_factory(formatd[name]).tostr))
fh, opened = cbook.to_filehandle(fname, 'w', return_opened=True)
writer = csv.writer(fh, delimiter=delimiter)
header = r.dtype.names
writer.writerow(header)
# Our list of specials for missing values
mvals = []
for name in header:
mvals.append(missingd.get(name, missing))
ismasked = False
if len(r):
row = r[0]
ismasked = hasattr(row, '_fieldmask')
for row in r:
if ismasked:
row, rowmask = row.item(), row._fieldmask.item()
else:
rowmask = [False] * len(row)
writer.writerow([func(val, mask, mval) for func, val, mask, mval
in zip(funcs, row, rowmask, mvals)])
if opened:
fh.close()