我有一个脚本可以读取XLS文件(用户输入),抓取数据并将该数据输出到.csv文件。
当我在一个XLS文件上运行脚本时,输出是正确的;但是,当我在另一个XLS文件上运行脚本时,脚本不会停在应该的位置,并将不必要的数据添加到输出文件中。
import csv, sys
from xlrd import open_workbook, XL_CELL_TEXT
input = raw_input('Input file: ')
#Place holder ID and Tank NO
#Final values will be taken from user input
termID="termid"
tankNO="tankno"
##Open *.xls file and assign sheet##
book = open_workbook(input)
sheet = book.sheet_by_index(0)
#Get Unit of Measure [(B)arrels/(G)allons] and starting row/cell
#Ranges 15 and 5 are arbitrary - used as a smallest possible range
#that will contain the first 'FT.' string
for row in range(15):
for col in range(5):
x = sheet.cell_value(row,col)
if x == 'FT.':
startCol = col+1
startRow = row+2
if str(sheet.cell_value(row, startCol)).lower() == 'barrels':
unit = 'B'
elif str(sheet.cell_value(row, startCol)).lower() == 'gallons':
unit = 'G'
break
#Grab Raw values from XLS and process based on UOM
rawData = []
for col in range(startCol, sheet.ncols, 2):
for row in range(startRow, sheet.nrows):
if isinstance(sheet.cell_value(row, col), float):
if unit == 'G':
if int(sheet.cell_value(row, col)) > 0:
tmp = round(sheet.cell_value(row, col))
rawData.append(str(int(tmp)).zfill(9))
elif unit == 'B':
if int(sheet.cell_value(row, col)) > 0:
tmp = ('%.2f' %sheet.cell_value(row, col))
tmp = str(tmp).replace(".", "").zfill(9)
rawData.append(tmp)
else:
break
#Break rawData into groups of 12
#And add to new list data[]
data = []
for i in xrange(0, len(rawData), 12):
data.append(rawData[i:i+12])
#Open csv file to write to
#test.csv will change to user input file name
outFile = open('test.csv', 'wb')
writeFile = csv.writer(outFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
#Add filler fields
#Max height is derived from length of data[] and data[-1]
#Foot increments are derived from length of data[]
#Tank Numober / Terminal ID are user input data
for i in xrange(len(data)):
data[i].insert(0, str(len(data)-1) + str(len(data[-1])-1).zfill(2)) #Max Height
data[i].insert(0, str(i).zfill(3)) #Foot increments
data[i].insert(0, tankNO) #Tank Number
data[i].insert(0, termID) #Terminal ID
#add empty strings to match length of all lists within data[]
#this only applies to the final list if it is less than 11"
if len(data[i]) < 16:
data[i].extend([''] * (16 - len(data[i])))
#The following three lines are filler and do not change
data[i].append(unit) #Add UOM
data[i].append('N') #Outage = N
data[i].append("") #Key Fill
writeFile.writerow(data[i])
outFile.close()
如果我使用找到here的输入文件,脚本将停止循环,并且输出文件完美无缺:
"termid","tankno","000","4502","000001486","000004563","000009365","000015074","000021275","000027625","000033978","000040332","000046685","000053039","000059392","000065745","G","N",""
"termid","tankno","001","4502","000072098","000078452","000084805","000091158","000097511","000103865","000110218","000116571","000122924","000129278","000135632","000141987","G","N",""
"termid","tankno","002","4502","000148342","000154570","000160704","000166786","000172836","000178865","000184885","000191240","000197595","000203950","000210304","000216659","G","N",""
"termid","tankno","003","4502","000223014","000229369","000235724","000242078","000248433","000254788","000261143","000267498","000273852","000280207","000286562","000292917","G","N",""
"termid","tankno","004","4502","000299272","000305626","000311981","000318336","000324689","000331043","000337396","000343749","000350103","000356456","000362810","000369163","G","N",""
"termid","tankno","005","4502","000375516","000381870","000388223","000394576","000400930","000407283","000413637","000419990","000426343","000432697","000439050","000445403","G","N",""
"termid","tankno","006","4502","000451757","000458110","000464463","000470817","000477170","000483524","000489877","000496230","000502584","000508937","000515290","000521644","G","N",""
"termid","tankno","007","4502","000527997","000534351","000540704","000547057","000553411","000559764","000566117","000572471","000578824","000585177","000591531","000597884","G","N",""
"termid","tankno","008","4502","000604237","000610591","000616944","000623297","000629650","000636004","000642357","000648710","000655063","000661417","000667770","000674123","G","N",""
"termid","tankno","009","4502","000680476","000686830","000693183","000699536","000705889","000712243","000718596","000724949","000731302","000737656","000744009","000750362","G","N",""
"termid","tankno","010","4502","000756715","000763069","000769422","000775775","000782128","000788482","000794835","000801188","000807541","000813895","000820248","000826601","G","N",""
"termid","tankno","011","4502","000832954","000839308","000845661","000852014","000858367","000864721","000871074","000877427","000883780","000890134","000896487","000902840","G","N",""
"termid","tankno","012","4502","000909193","000915547","000921900","000928253","000934606","000940960","000947313","000953666","000960019","000966373","000972726","000979079","G","N",""
"termid","tankno","013","4502","000985432","000991786","000998139","001004492","001010845","001017199","001023552","001029905","001036258","001042612","001048965","001055318","G","N",""
"termid","tankno","014","4502","001061671","001068025","001074378","001080731","001087084","001093438","001099791","001106144","001112497","001118851","001125204","001131557","G","N",""
"termid","tankno","015","4502","001137910","001144264","001150617","001156970","001163323","001169677","001176030","001182383","001188736","001195090","001201443","001207796","G","N",""
"termid","tankno","016","4502","001214152","001220516","001226879","001233243","001239607","001245970","001252334","001258698","001265062","001271425","001277789","001284153","G","N",""
"termid","tankno","017","4502","001290517","001296880","001303244","001309608","001315971","001322335","001328699","001335063","001341426","001347790","001354154","001360518","G","N",""
"termid","tankno","018","4502","001366881","001373245","001379609","001385973","001392336","001398700","001405064","001411427","001417791","001424155","001430519","001436882","G","N",""
"termid","tankno","019","4502","001443246","001449610","001455974","001462337","001468701","001475065","001481428","001487792","001494156","001500520","001506883","001513247","G","N",""
"termid","tankno","020","4502","001519611","001525975","001532338","001538702","001545066","001551429","001557793","001564157","001570521","001576884","001583248","001589612","G","N",""
"termid","tankno","021","4502","001595976","001602339","001608703","001615067","001621431","001627794","001634158","001640522","001646885","001653249","001659613","001665977","G","N",""
"termid","tankno","022","4502","001672340","001678704","001685068","001691432","001697795","001704159","001710523","001716886","001723250","001729614","001735978","001742341","G","N",""
"termid","tankno","023","4502","001748705","001755069","001761433","001767796","001774160","001780524","001786887","001793251","001799615","001805979","001812342","001818706","G","N",""
"termid","tankno","024","4502","001825070","001831438","001837805","001844172","001850540","001856907","001863275","001869642","001876009","001882377","001888744","001895111","G","N",""
"termid","tankno","025","4502","001901479","001907846","001914213","001920581","001926948","001933316","001939683","001946050","001952418","001958785","001965152","001971520","G","N",""
"termid","tankno","026","4502","001977887","001984254","001990622","001996989","002003356","002009724","002016091","002022459","002028826","002035193","002041561","002047928","G","N",""
"termid","tankno","027","4502","002054295","002060663","002067030","002073397","002079765","002086132","002092500","002098867","002105234","002111602","002117969","002124336","G","N",""
"termid","tankno","028","4502","002130704","002137071","002143438","002149806","002156173","002162540","002168908","002175275","002181643","002188010","002194377","002200745","G","N",""
"termid","tankno","029","4502","002207112","002213479","002219847","002226214","002232581","002238949","002245316","002251684","002258051","002264418","002270786","002277153","G","N",""
"termid","tankno","030","4502","002283520","002289888","002296255","002302622","002308990","002315357","002321724","002328092","002334459","002340827","002347194","002353561","G","N",""
"termid","tankno","031","4502","002359929","002366296","002372663","002379031","002385398","002391765","002398133","002404500","002410868","002417235","002423602","002429970","G","N",""
"termid","tankno","032","4502","002436336","002442699","002449062","002455425","002461788","002468151","002474514","002480877","002487239","002493602","002499965","002506328","G","N",""
"termid","tankno","033","4502","002512691","002519054","002525417","002531780","002538143","002544506","002550869","002557232","002563595","002569958","002576321","002582684","G","N",""
"termid","tankno","034","4502","002589047","002595410","002601773","002608136","002614499","002620862","002627225","002633588","002639951","002646313","002652676","002659039","G","N",""
"termid","tankno","035","4502","002665402","002671765","002678128","002684491","002690854","002697217","002703580","002709943","002716306","002722669","002729032","002735395","G","N",""
"termid","tankno","036","4502","002741758","002748121","002754484","002760847","002767210","002773573","002779936","002786299","002792662","002799025","002805387","002811750","G","N",""
"termid","tankno","037","4502","002818113","002824476","002830839","002837202","002843565","002849928","002856291","002862654","002869017","002875380","002881743","002888106","G","N",""
"termid","tankno","038","4502","002894469","002900832","002907195","002913558","002919921","002926284","002932647","002939010","002945373","002951736","002958099","002964461","G","N",""
"termid","tankno","039","4502","002970824","002977187","002983550","002989913","002996276","003002639","003009002","003015365","003021728","003028091","003034454","003040817","G","N",""
"termid","tankno","040","4502","003047183","003053556","003059930","003066303","003072676","003079050","003085423","003091797","003098170","003104543","003110917","003117290","G","N",""
"termid","tankno","041","4502","003123664","003130037","003136410","003142784","003149157","003155531","003161904","003168277","003174651","003181024","003187398","003193771","G","N",""
"termid","tankno","042","4502","003200144","003206518","003212891","003219265","003225638","003232011","003238385","003244758","003251132","003257505","003263878","003270252","G","N",""
"termid","tankno","043","4502","003276625","003282999","003289372","003295745","003302119","003308492","003314866","003321239","003327612","003333986","003340359","003346733","G","N",""
"termid","tankno","044","4502","003353106","003359479","003365853","003372226","003378600","003384973","003391346","003397720","003404093","003410467","003416840","003423213","G","N",""
"termid","tankno","045","4502","003429587","003435960","003442334","","","","","","","","","","G","N",""
如果我使用找到here的输入文件,脚本不会停在应该的位置,并在输出文件中添加一行:
"termid","tankno","000","4601","000001361","000004484","000009876","000017702","000028069","000040996","000056543","000074365","000093986","000115294","000138264","000162638","B","N",""
"termid","tankno","001","4601","000187949","000213802","000239910","000266076","000292243","000318410","000344576","000370743","000396910","000423077","000449244","000475411","B","N",""
"termid","tankno","002","4601","000501578","000527748","000553917","000580087","000606257","000632428","000656506","000676640","000693187","000706506","000716956","000724896","B","N",""
"termid","tankno","003","4601","000730684","000753111","000779276","000805441","000831607","000857772","000883937","000910102","000936267","000962431","000988595","001014758","B","N",""
"termid","tankno","004","4601","001040922","001067085","001093249","001119412","001145576","001171740","001197903","001224067","001250230","001276394","001302557","001328721","B","N",""
"termid","tankno","005","4601","001354885","001381048","001407212","001433375","001459539","001485702","001511866","001538030","001564193","001590357","001616520","001642684","B","N",""
"termid","tankno","006","4601","001668847","001695011","001721175","001747338","001773502","001799665","001825829","001851992","001878156","001904320","001930483","001956647","B","N",""
"termid","tankno","007","4601","001982810","002008974","002035137","002061301","002087464","002113628","002139799","002165990","002192182","002218373","002244565","002270756","B","N",""
"termid","tankno","008","4601","002296948","002323139","002349331","002375522","002401714","002427906","002454097","002480289","002506480","002532672","002558863","002585055","B","N",""
"termid","tankno","009","4601","002611246","002637438","002663629","002689821","002716013","002742204","002768396","002794587","002820779","002846970","002873162","002899353","B","N",""
"termid","tankno","010","4601","002925545","002951736","002977928","003004119","003030311","003056503","003082694","003108886","003135077","003161269","003187460","003213652","B","N",""
"termid","tankno","011","4601","003239843","003266035","003292226","003318418","003344609","003370801","003396993","003423184","003449376","003475567","003501759","003527950","B","N",""
"termid","tankno","012","4601","003554142","003580333","003606525","003632716","003658908","003685099","003711291","003737483","003763674","003789866","003816057","003842249","B","N",""
"termid","tankno","013","4601","003868440","003894632","003920823","003947015","003973206","003999398","004025590","004051781","004077973","004104164","004130356","004156547","B","N",""
"termid","tankno","014","4601","004182739","004208930","004235122","004261313","004287505","004313696","004339888","004366080","004392271","004418463","004444654","004470846","B","N",""
"termid","tankno","015","4601","004497037","004523229","004549420","004575612","004601803","004627995","004654189","004680389","004706589","004732790","004758990","004785191","B","N",""
"termid","tankno","016","4601","004811391","004837591","004863792","004889992","004916192","004942393","004968593","004994793","005020994","005047194","005073395","005099595","B","N",""
"termid","tankno","017","4601","005125795","005151996","005178196","005204396","005230597","005256797","005282998","005309198","005335398","005361599","005387799","005413999","B","N",""
"termid","tankno","018","4601","005440200","005466400","005492601","005518801","005545001","005571202","005597402","005623602","005649803","005676003","005702203","005728404","B","N",""
"termid","tankno","019","4601","005754604","005780805","005807005","005833205","005859406","005885606","005911806","005938007","005964207","005990408","006016608","006042808","B","N",""
"termid","tankno","020","4601","006069009","006095209","006121409","006147610","006173810","006200011","006226211","006252411","006278612","006304812","006331012","006357213","B","N",""
"termid","tankno","021","4601","006383413","006409613","006435814","006462014","006488215","006514415","006540615","006566816","006593016","006619216","006645417","006671617","B","N",""
"termid","tankno","022","4601","006697818","006724018","006750218","006776419","006802619","006828819","006855020","006881220","006907420","006933621","006959821","006986022","B","N",""
"termid","tankno","023","4601","007012222","007038422","007064623","007090823","007117023","007143224","007169427","007195639","007221851","007248062","007274274","007300486","B","N",""
"termid","tankno","024","4601","007326698","007352910","007379122","007405333","007431545","007457757","007483969","007510181","007536392","007562604","007588816","007615028","B","N",""
"termid","tankno","025","4601","007641240","007667451","007693663","007719875","007746087","007772299","007798510","007824722","007850934","007877146","007903358","007929570","B","N",""
"termid","tankno","026","4601","007955781","007981993","008008205","008034417","008060629","008086840","008113052","008139264","008165476","008191688","008217899","008244111","B","N",""
"termid","tankno","027","4601","008270323","008296535","008322747","008348959","008375170","008401382","008427594","008453806","008480018","008506229","008532441","008558653","B","N",""
"termid","tankno","028","4601","008584865","008611077","008637288","008663500","008689712","008715924","008742136","008768347","008794559","008820771","008846983","008873195","B","N",""
"termid","tankno","029","4601","008899407","008925618","008951830","008978042","009004254","009030466","009056677","009082889","009109101","009135313","009161525","009187736","B","N",""
"termid","tankno","030","4601","009213948","009240160","009266372","009292584","009318795","009345007","009371219","009397431","009423643","009449855","009476066","009502278","B","N",""
"termid","tankno","031","4601","009528490","009554702","009580914","009607125","009633337","009659549","009685762","009711977","009738192","009764407","009790622","009816837","B","N",""
"termid","tankno","032","4601","009843052","009869267","009895482","009921697","009947912","009974128","010000343","010026558","010052773","010078988","010105203","010131418","B","N",""
"termid","tankno","033","4601","010157633","010183848","010210063","010236278","010262493","010288708","010314924","010341139","010367354","010393569","010419784","010445999","B","N",""
"termid","tankno","034","4601","010472214","010498429","010524644","010550859","010577074","010603289","010629505","010655720","010681935","010708150","010734365","010760580","B","N",""
"termid","tankno","035","4601","010786795","010813010","010839225","010865440","010891655","010917870","010944085","010970301","010996516","011022731","011048946","011075161","B","N",""
"termid","tankno","036","4601","011101376","011127591","011153806","011180021","011206236","011232451","011258666","011284882","011311097","011337312","011363527","011389742","B","N",""
"termid","tankno","037","4601","011415957","011442172","011468387","011494602","011520817","011547032","011573247","011599462","011625678","011651893","011678108","011704323","B","N",""
"termid","tankno","038","4601","011730538","011756753","011782968","011809183","011835398","011861613","011887828","011914043","011940259","011966474","011992689","012018904","B","N",""
"termid","tankno","039","4601","012045119","012071334","012097549","012123764","012149979","012176194","012202412","012228636","012254861","012281085","012307310","012333535","B","N",""
"termid","tankno","040","4601","012359759","012385984","012412208","012438433","012464657","012490882","012517107","012543331","012569556","012595780","012622005","012648229","B","N",""
"termid","tankno","041","4601","012674454","012700679","012726903","012753128","012779352","012805577","012831801","012858026","012884251","012910475","012936700","012962924","B","N",""
"termid","tankno","042","4601","012989149","013015373","013041598","013067822","013094047","013120272","013146496","013172721","013198945","013225170","013251394","013277619","B","N",""
"termid","tankno","043","4601","013303844","013330068","013356293","013382517","013408742","013434966","013461191","013487416","013513640","013539865","013566089","013592314","B","N",""
"termid","tankno","044","4601","013618538","013644763","013670988","013697212","013723437","013749661","013775886","013802110","013828335","013854560","013880784","013907009","B","N",""
"termid","tankno","045","4601","013933233","013959458","013985682","014011907","014038132","014064356","014090581","014116805","014143030","014169254","014195479","014221704","B","N",""
"termid","tankno","046","4601","000000600","000001200","","","","","","","","","","","B","N",""
如果将此第二个输出与输入文件进行比较,则不应包含输出文件的最后一行,并且当包含它时,第5列是错误的。
答案 0 :(得分:0)
进行一些测试并想出一个解决方案 - 可能不是最好的,但它现在正在运行:
我在脚本开始抓取数据(check
)之前添加了一个变量line 32
,该数据最初设置为0.脚本检查当前单元格是否为float
后,检查单元格的值是否大于check
。如果它大于check
,请将值添加到rawData[]
并将check
设置为单元格的值。这样,check
将始终等于rawData[-1]
,因此当需要数据结束时,会忽略任何低于添加到列表中的最后一个数字的内容。
#Grab Raw values from XLS and process based on UOM
rawData = []
check = 0
for col in range(startCol, sheet.ncols, 2):
for row in range(startRow, sheet.nrows):
if isinstance(sheet.cell_value(row, col), float):
if sheet.cell_value(row, col) > check:
if unit == 'G':
print type(sheet.cell_value(row, col)), " - ", sheet.cell_value(row, col)
tmp = round(sheet.cell_value(row, col))
rawData.append(str(int(tmp)).zfill(9))
check = int(sheet.cell_value(row, col))
elif unit == 'B':
print type(sheet.cell_value(row, col)), " - ", sheet.cell_value(row, col)
tmp = ('%.2f' %sheet.cell_value(row, col))
tmp = str(tmp).replace(".", "").zfill(9)
rawData.append(tmp)
check = int(sheet.cell_value(row, col))
else:
break