我正在尝试为我的结果添加打印到文本文件以及显示的cmd行列表。并尝试使用with open('output.txt', 'w') as f:
。虽然代码已经使用with open(lic) as f:
。我可以使用多个"使用open"声明?除了cmd行列表外,如何生成文件文本打印输出?
import time
fips_dict = {}
fips_dict["02000"]="AK"
fips_dict["02013"]="AK, Aleutians East"
fips_dict["02016"]="AK, Aleutians West"
while 1: #repeat forever
count = 0
lic = "//Server/replicated/Userdata/"+raw_input("Account: ")+"/Dmp/LicenseStyle.xml"
print
try: #error handling
with open(lic) as f:
for line in f:
#get and print fips codes and county names
if 'RampEntry' in line:
count += 1
fips_index = line.index('dataValue')+11
code = line[fips_index : fips_index+5]
statecode = line[fips_index : fips_index+2]
if code.isalnum():
print code,fips_dict[code]
elif statecode.isalpha():
print "Entire State of", statecode
print
else:
print "format error in", lic
except IOError: #if file not found
print lic[23:],"not found"
except KeyError: #if no match for FIPS
print "not found"
except:
print "an unexpected error has occured!"
print
now = time.strftime("%c")
print ("%s" % now )
print