Python在处理字符串时遇到了问题,还没达到判定条件就执行了语句,是不是计算机底层的原因?还是Python的性质导致的?
import sys
from gapfilling_function import Gapfilling
test = Gapfilling('GP_turbine.txt')
for i in range(len(test.get_items())):
print(test.get_items()[i].rstrip())
print('************************************************************************************************')
print('************************************************************************************************')
for qst in test.get_questions():
print(qst.rstrip())
下面的是判定条件,在没达到判定条件时就开始显示 ? 了
def get_question(self, line):
theline = line
flag = False
flag_temp = False
length = len(theline)
for i in range(length):
if (theline[i] != '\n') and (i <= length):
if theline[i] == '(' and flag == False and flag_temp == False:
flag = True
flag_temp = True
if theline[i] != ')' and flag == True and flag_temp == False:
theline = theline.replace(theline[i], '?')
flag_temp = False
if theline[i] == ')':
flag = False
return theline
401. (偏心)是指轴表面外径与轴真实几何中心线之间的变化,这种变化亦称为轴弯曲。
402. 锅炉给水泵是火电厂的重要辅助设备之一,当锅炉给水泵的流量小于要求的(最小流量)时,流过给水泵的给水会局部汽化,导致产生汽蚀而损坏给水泵。
403. 为确保阀门活动灵活,需定期对阀门进行(活动)试验,以防止卡涩。
404. 高压加热器是利用汽轮机抽汽来加热(给水)。
405. 当高压加热器水位高于高II值时,应当迅速开启(事故疏水)阀。
406. 旁路系统是指高参数(蒸汽)不进入汽缸的通流部分作功,而是经过与汽缸并联的(减温减压器)将降压减温后的蒸汽送至低一级参数的蒸汽管道或凝汽器去的连接系统。
************************************************************************************************
************************************************************************************************
1. TSI汽轮机监测显示系统主要对汽机(??)、(??)、(??)等起到监测显示作用。
2. 按传热方?不同,回热加热器可分为(???)和(???)两种。
3. 按汽机排汽凝结的方?不同,凝汽器可分成(???)和(???)两种。
4. 备用冷油器的进口油门(??),出口油门(??),冷却水入口门(??),出口门(??)。
5. 泵的????分为(??????)、(??????)。
正常打印文件是没有问题的,完全显示正常。
执行结果的双星号下面的执行结果,在没达到判定条件时显示?很不能理解,处理语句感觉没有问题,大家帮忙检查一下
def get_question(self, line):
theline = line
flag = False
flag_temp = False
length = len(theline)
for i in range(length):
if theline[i] != '\n':
if theline[i] == '(' and not flag and not flag_temp:
flag = True
flag_temp = True
elif theline[i] != ')' and flag and not flag_temp:
theline = theline.replace(theline[i], '?')
flag_temp = False
if theline[i] == ')':
flag = False
return theline
修改成上面的试试呢
改成这样行不行?
def get_question(line):
theline = line
list1 = re.findall(r"(?<=\().+?(?=\))", theline)
list1 = ['(' + i + ')' for i in list1]
# print(list1)
for i in list1:
if i in theline:
# print(theline[theline.find(i):theline.find(i) + len(i)], len(i))
theline = theline.replace(theline[theline.find(i):theline.find(i) + len(i)], '(' + '?' * (len(i)-2) + ')')
# print(theline)
return theline
你的函数没看懂啊,你全程的局部变量是赋值了,可是整个for写了根没写是一样的,最终不还是返回同样的一个值吗,要是你想返回不同的值,是不是还得在for那里面加上多一个return语句