我有一些正在读取 csv 文件的代码。然后代码根据用户输入添加值。我遇到的问题是删除包含无效值的条目。特定的无效值将是 M。我在 csv 文件中使用 M 来表示缺失。所以基本上我想做的是让用户输入开始和结束月份,然后让代码加起来降水值。但是,如果字符串应该包含一个 MI 并不想包含该行数据。例如...下面包含的部分示例。
Station, Stat, lat, lon, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG
Bainville 6 NE, 24-0408-06, 48.14065, -104.267, 0.10, 0.01, 0.12, 1.23, 0.02, 0.34, M, 0.00
Brockton 20S, 24-1164-06, 47.5075, -104.324, M, 0.08, 0.13, 1.54, 2.43, 1.23, 1.12, 0.9
Cohagen, 24-1875-06, 47.564, -106.7, 0.3, 0.37, M, 0.76, 1.55, 1.69, 0.35, 0.41
Sidney, 24-7560-06, 47.36, -104.47, 0.1, 0.21, 0.05, 1.21, M, 1.25, 2.75, 0.89
现在,如果用户要选择从 jan 到 mar 的月份,那么我希望发生的是 Brockten 行(jan)和 Cohagen 行(mar)被省略,因为值为 M。但是,如果用户选择了 apr 到可能的月份那么要省略的行将是 Sidney。
我希望这是有道理的。我知道这篇文章已经很长了,但我也会包含我的代码。
##################################################
import csv
import array
import decimal
decimal.getcontext().prec = 4
from time import gmtime, strftime
print strftime("%Y-%m-%d %H:%M:%S", gmtime())
# Create an unordered MON to column number dictionary and get user data
mdict = {'MAR': 11, 'FEB': 10, 'AUG': 16, 'SEP': 17, 'APR': 12, 'JUN': 14,
'JUL': 15, 'JAN': 9, 'MAY': 13, 'NOV': 19, 'DEC': 20, 'OCT': 18}
month_start = raw_input('Input the 3 letter ID of beginning month: ')
month_end = raw_input('Input the 3 letter ID of ending month: ')
month_start = month_start.upper()
month_end = month_end.upper()
mon_layer_name = month_start + ' through ' +month_end
user_month = '[' + mon_layer_name + ']'
start_num = mdict[month_start]
end_num = mdict[month_end]+1
new_list = [['Station', 'Lat', 'Long', 'mysum']]
with open('R:\\COOP\\temp\\COOP_rainfall2.csv', 'rb') as csvfile:
filereader = csv.reader(csvfile)
filereader.next() # this is to skip header
for row in filereader:
#print row
sta = row[0]
lat = row[2]
lon = row[3]
tot = decimal.Decimal(0)
for x in row[start_num:end_num]:
print 'now in line 34 in code'
if x == '': x = 0
elif x == 'M': # I think this is where I need to do something just not sure how ot accomplish it.
x = 0
print row
tot = tot + decimal.Decimal(x)
if tot == 0: continue
else: new_list.append([sta, lat, lon, str(tot)])
with open('R:\\COOP\\temp\\output.csv', 'wb') as csvout:
print 'Now in file writer code block'
filewriter = csv.writer(csvout)
for line in new_list:
filewriter.writerow(line)
Rex = 'R:\\COOP\\temp\\output.csv'
Precip=[] #creating an array named Precip
inp = open (Rex,"r")
for line in inp.readlines():
line.split(',')
Precip.append(line)
file.close(inp)
print 'End of code'
任何帮助是极大的赞赏。