我想优化以下代码:
for myFile in myFiles:
file = open(filename, 'rt')
try:
if CLIENT == "C1":
head = rows[:7]
tail = rows[7:]
for row in rows:
if "".join(row)!= "":
if not u_pass:
header = [ row.strip().replace(" ", "_") for row in row[3:] ]
u_pass = True
else:
self.usecases(row, data, index)
elif CLIENT == 'C2':
reader = csv.reader(file)
firstline = next(reader)
secondline = next(reader)
else:
for row in rows:
if "".join(row)!= "":
if not u_pass:
header = [ row.strip().replace(" ", "_") for row in row[3:] ]
u_pass = True
# Recuperation des donnees
else:
self.usecases(row, data, index)
以下代码在前面的代码中重复了两次,这意味着这些条件之间有一些共同的指令"
for row in rows:
if "".join(row)!= "":
if not u_pass:
header = [ row.strip().replace(" ", "_") for row in row[3:] ]
u_pass = True
else:
self.usecases(row, data, index)
答案 0 :(得分:1)
下面应该做的诀窍:
for myFile in myFiles:
file = open(filename, 'rt')
try:
if CLIENT == "C2":
reader = csv.reader(file)
firstline = next(reader)
secondline = next(reader)
else:
if CLIENT == "C1":
head = rows[:7]
tail = rows[7:]
for row in rows:
if "".join(row)!= "":
if not u_pass:
header = [ row.strip().replace(" ", "_") for row in row[3:] ]
u_pass = True
else:
self.usecases(row, data, index)