如何解决SyntaxError:with 语句套件中的语法无效
我正在运行tweak_starfile.py,它从CSV 文件中读取有关附近恒星的数据,并将数据转换为适合我正在开发的明星交易者类型游戏的形式。我在 IDLE 中运行程序。根据良好的实践,我将“按摩”代码放在 with 语句中。然而,在 with 套件中,f.close()
生成一个 SyntaxError
。我究竟做错了什么?据我所知,语法是正确的。
#!/usr/bin/env python3
# file: tweak_starfile.py
# author: Rick Darwin (c) 2018,2019,2020
import math
import re
import pickle
from collections import namedtuple
RAdpar = namedtuple("RAdpar","RA2k dec parallax")
Polar3 = namedtuple("Polar3","theta phi rho")
Vectsm = namedtuple("Vectsm","sec_per_year motion_angle")
Rect3 = namedtuple("Rect3","x y z")
Stella = namedtuple("Stella","nomen idcodes spectral colour abs_mag est_mass num_obj rect3 note")
def tweak_starfile(starcount,csvfile='RECONSNearest110.csv',starfile='starfile_001.dat'):
""" Read one line from csvfile,unpack to a tuple,massage the data,append a new Stella() namedtuple to list starcat. Repeat till starcount
records are done,then pickle the resultant starcat.
"""
with open(csvfile,'r') as f:
starcat = []
linect = 0
for line in f.readlines():
if linect == 0:
pass # ignore the column heads
else:
if linect >= starcount:
print('{} records read from {}. '.format(linect,csvfile)) #DEBUG
break
#. end if
# Read in the data
# Massage the data
# Make a new tuple
#. Save the record to the catalog
#. end for
f.close() # *** SyntaxError: invalid Syntax ***
#. end with
# Write out the data
print('Writing out the data to ./starfile')
with open(starfile,'wb') as f:
pickle.dumps(starcat,f,protocol=pickle.HIGHEST_PROTOCOL)
f.close()
#. end with
#. end def
def hms2hhhh(hms):
""" Convert a hms string like '20:30:40.1' or '06 45 08.9' into decimal hours
(without ValueError on 09 or 08.9,(ie,not treated as oct!)
"""
suite
#. end def
def hhh2rad(hhh):
""" Convert decimal hours to radians
"""
suite
#. end def
def starcolour(spectra):
""" Return a 3tuple from a color picker; spectral holds a string like 'M5.0 V'
"""
print('In starcolour')
sc = spectra[0]
dicto = {
'O': (0,153,255),# blue
'B': (102,204,# blue-white
'A': (255,255,# white
'F': (255,153),# yellow-white
'G': (255,0),# yellow
'K': (255,# orange
'M': (192,51,# red
'D': (255,0) # deep red
}
return dicto[sc] if sc in dicto.keys() else (0,255) # cyan! why not?
#. end def
if __name__ == '__main__':
tweak_starfile(110,"RECONSNearest110.csv","starfile_001.dat")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。