我有一个(丑陋的)代码,几乎可以完成工作,但似乎在某个地方绊了一下。基本上,我有一个来自不同目录的文件名文本文件,我希望我的脚本读取文件的每一行,如果它包含给定的字符串,请在新文件中打印一个对应的数字。对于上下文,我们将样本分成20个袋子,每个袋子的样本名称为“ G-17 ###”,并对每个样本进行多次测量,因此我希望脚本将“ G- what”名称识别为字符串,并且如果该编号属于给定的袋子,则打印它所属的袋子的编号。到目前为止,代码正在打开文件,读取字符串并创建并打印到指定的输出文件。问题在于输出文件仅包含1,而不是应包含的1到20。
我认为我只是忽略了代码中的细微调整,因此可以解决此问题,但是我不确定该调整将是什么。
def cleanname():
out = 'cleanednames.txt'
with open('matlabnames.txt', 'r') as namefile, open(out, 'w') as cleannames:
for line in namefile:
if 'g17198' or 'g17199' or 'g17200' or 'g17201' or 'g17202' or 'g17203' or 'g17204' or 'g17205' or 'g17206' or 'g17207' or 'g17208' or 'g17209' in line:
cleannames.write(str(1)+'\n')
elif 'G-17214' or 'g17210' or 'g17211' or 'g17212' or 'g17213' or 'g17214' or 'g17215' or 'g17216' or 'g17217' or 'g17218' or 'g17219' or 'g17220' in line:
cleannames.write(str(2)+'\n')
elif 'Bag3' or 'G12225' or 'G12226' or 'G17221' or 'G17222' or 'G17223' or 'G17224' in line:
cleannames.write(str(3)+'\n')
elif 'Bag4' or 'G-17229' or 'G-17231' or 'G-17232' or 'G-17234' or 'G-17237' or 'G17229' or 'G17230' or 'G17231' or 'G17232' or 'G17233' or 'G17234' or 'G17235' or 'G17236' or 'G17237' or 'G17238' or 'G17239' in line:
cleannames.write(str(4)+'\n')
elif 'g17240' or 'g17241' or 'g17242' or 'g17243' or '"g17243' or 'g17244' or 'g17245' or 'G17245' or 'g17246' or 'G17246' or 'g17247' or 'g17248' or 'g17249' or 'G17249' or 'g17250' or 'g17251' or 'G17251' or 'g17252' or 'g17253' or 'g17254' or 'g17255' or 'G17255' in line:
cleannames.write(str(5)+'\n')
elif 'Bag6' or 'g17256' or 'g17257' or 'g17258' or 'g17259' or 'g17260' or 'g17261' or 'g17262' or 'g17263' or 'g17264' or 'g17265' or 'g17266' or 'g17267' or 'g17268' in line:
cleannames.write(str(6)+'\n')
elif 'G-17270' or 'G-17277' or 'G-17281' or 'G-17282' or 'G-17283' or 'G17270' or 'G17271' or 'G17272' or 'G17273' or 'G17274' or 'G17275' or 'G17276' or 'G17277' or 'G17278' or 'G17279' or 'G17280' or 'G17281' or 'G17282' or 'G17283' in line:
cleannames.write(str(7)+'\n')
elif 'G-17286' or 'G-17289' or 'G-17290' or 'G-17291' or 'G-17292' in line:
cleannames.write(str(8)+'\n')
elif 'Bag9' or 'g17295' or 'G17296' or 'G17297' or 'G17298' or 'G17299' or 'G17300' in line:
cleannames.write(str(9)+'\n')
elif 'G-17310' or 'G-17311' or'G-17312' or'G-17313' or'G-17316' or 'G- 17310' or 'G- 17311' or'G- 17312' or'G- 17313' or'G- 17316' or 'G17301' or 'G17302' or 'G17303' or 'G17304' or 'G17305' or 'G17306' or 'G17307' or 'G17308' or 'G17309' or 'G17310' or 'G17311' or 'G17312' or 'G17313' or 'G17314' or 'G17315' or 'G17316' or 'G17317' in line:
cleannames.write(str(10)+'\n')
elif 'Bag11' or 'G-17318' or 'G-17319' or 'G-17321' or 'G-17322' or 'G-17323' or 'G17318' or 'G17319' or 'G17320' or 'G17321' or 'G17322' or 'G17323' or 'G17324' or 'G17325' in line:
cleannames.write(str(11)+'\n')
elif 'Bag12' or 'G- 17328' or 'G- 17331' or 'G- 17332' or 'G- 17333' or 'G- 17335' or 'G-17328' or 'G-17331' or 'G-17332' or 'G-17333' or 'G-17335' or 'g17326' or 'g17327' or 'g17328' or 'G17328' or 'g17329' or 'g17330' or 'g17331' or 'G17331' or 'g17332' or 'G17332' or 'g17333' or 'G17333' or 'g17334' or 'g17335' or 'G17335' or 'g17336' or 'g17337' or 'g17338' in line:
cleannames.write(str(12)+'\n')
#elif 'X' in line:
# cleannames.write(str(13)+'\n')
elif 'Bag14' or 'g17347' or 'g17348' or 'g17349' or 'g17350' or 'g17351' or 'g17352' or 'g17353' or 'g17354' or 'g17355' or 'g17356' or 'g17357' or 'G17357' or 'g17358' or 'G17359' or 'g17360' or 'G17360' or 'g17361' or 'G17361' or 'g17362' or 'G17362' or 'g17363' or 'G17363' in line:
cleannames.write(str(14)+'\n')
elif 'G17366' or 'G17369' or 'G17370' or 'G17374' or 'G17376' in line:
cleannames.write(str(15)+'\n')
elif 'G17380' or 'G17381' or 'G17383' or 'G17388' or 'G17392' in line:
cleannames.write(str(16)+'\n')
elif 'G17393' or 'G17397' or'G17399' or'G17400' or'G17401' or'G17402' or'G17403' in line:
cleannames.write(str(17)+'\n')
elif 'Bag18' or 'G17404' or 'G17405' or 'G17406' or 'G17407' or 'G17408' or 'G17409' or 'G17410' or 'G17411' or 'G17412' or 'G17413' or 'G17414' or 'G17415' or 'G17416' or 'G17417' in line:
cleannames.write(str(18)+'\n')
elif 'G17424' or 'G17425' or 'G17428' or 'G17430' or 'G17431' in line:
cleannames.write(str(19)+'\n')
#elif 'X' in line:
# cleannames.write(str(20)+'\n')
else:
print('There was a problem renaming line ' + line)
print('File names are cleaned and ready to upload to MATLAB.')
##Note: If the names are changed correctly, from top to bottom, the class order should be:
##19, 18, 17, 16, 15, 14, 12, 11, 10, 9, 7, 6, 5, 4, 3, 2, 1, 3, 12, 11, 8, 7, 4, 2, 12, 10, 9, 6, 4, 3, 18, 14, 12, 11
cleanname()
预期的结果应该是代码中指定的顺序(在“注释”部分中)具有一定数量的重复数字。该代码一直运行,没有错误消息。