请把文件内的perl翻译为python3.7
时间: 2024-10-08 21:19:53 浏览: 30
python3.7、Anaconda安装包.txt
将Perl脚本转换成Python 3.7并不是一个一对一的直接替换过程,因为两者在语法和一些函数调用上有较大差异。以下是根据提供的Perl脚本的主要逻辑,使用Python 3.7进行的一个大致等效实现:
```python
def deal():
Ori_FC = [""] * 4 # 假设外部已经给Ori_FC赋值了
ticketid = Ori_FC[0]
ticketno = Ori_FC[1]
fc_data = Ori_FC[3]
if "END" not in fc_data:
print(f"{ERR}{ticketid},{ticketno},{fc_data}")
if "XT" not in fc_data:
print(fc_data)
else:
FC = fc_data[:fc_data.index("XT")]
print(f"{ticketid},{FC}")
else:
FC = fc_data[:fc_data.index("END")]
if "/IT" in FC or "/BT" in FC:
print(f"{ERR}{ticketid},{ticketno},{fc_data}")
elif "(" in FC or ")" in FC:
print(f"{ERR}{ticketid},{ticketno},{fc_data}")
elif "." not in FC:
print(f"{ERR}{ticketid},{ticketno},{fc_data}")
else:
pos_dash = FC.find("//")
while pos_dash != -1:
FC = f"{FC[:pos_dash]} // {FC[pos_dash + 2:]}"
pos_dash = FC.find("//", pos_dash + 3)
pos_dash = FC.find("/-")
while pos_dash != -1:
FC = f"{FC[:pos_dash]} // {FC[pos_dash + 2:]}"
pos_dash = FC.find("/-", pos_dash + 3)
pos_dot = FC.find(".")
while pos_dot != -1:
FC = f"{FC[:pos_dot+3]} {FC[pos_dot+3:]}"
pos_temp = pos_dot - 1
while pos_temp >= 0 and FC[pos_temp].isdigit():
pos_temp -= 1
FC = f"{FC[:pos_temp+1]}{FC[pos_temp+1:pos_dot+3]} {FC[pos_dot+3:]}"
pos_dot = FC.find(".", pos_dot + 3)
keywords = ["FC/", "FC", "T-", "A-", "B-", "M/", "X/", "E/", "IN/", "A/", " M ", " Q ", " 5M ", " 10M "]
for keyword in keywords:
FC = FC.replace(keyword, "")
pos_dash = FC.find("-")
while pos_dash != -1:
next_pos_space = FC.find(" ", pos_dash)
if next_pos_space != -1:
if pos_dash == 0:
FC = FC[next_pos_space + 1:]
else:
FC = f"{FC[:pos_dash-1]}{FC[next_pos_space+1:]}"
else:
if pos_dash == 0:
FC = ""
else:
FC = FC[:pos_dash-1]
pos_dash = FC.find("-", pos_dash + 1)
FC = ' '.join(FC.split())
fctmp = [item for item in FC.strip().split(' ') if item]
totalare = float(fctmp[-2])
cny = fctmp[-1]
del fctmp[-2:]
for i, item in enumerate(fctmp):
if re.match(r'^\d+\.\d+$', item): # is amount
tempamt = float(item)
for j in range(i + 1, len(fctmp)):
if re.match(r'^\d+\.\d+$', fctmp[j]): # is amount
tempamt += float(fctmp[j])
fctmp[j] = ""
elif len(fctmp[j]) != 2: # not an amount and not a carrier code
fctmp[j] = ""
elif len(fctmp[j+1]) != 3: # not an amount carrier code followed by airport code
fctmp[j] = ""
else:
break
fctmp[i] = "{:.02f}".format(tempamt)
elif len(item) > 3:
fctmp[i] = ""
result = []
segno = 1
fareno = 1
totalseg = 1
p1 = 0
p2 = 0
airptStr = ""
airptOCStr = ""
totalAirptOCStr = ""
for i in range(len(fctmp)-1):
if re.match(r'^\d+\.\d+$', fctmp[i+1]):
f1 = fctmp[i+1]
for j in range(p1, p2):
result[j][7] = f1
result[j][11] = airptOCStr
result[j][12] = airptStr
segno = 1
fareno += 1
p1 = p2
fctmp[i+1] = fctmp[i]
airptStr = ""
airptOCStr = ""
else:
result.append([ticketid, ticketno, fareno, segno, fctmp[i], fctmp[i+1], fctmp[i+2], totalare, cny, totalseg])
if not airptStr:
airptStr = f"{fctmp[i]}-{fctmp[i+2]}"
else:
airptStr += f"-{fctmp[i+2]}"
if not airptOCStr:
airptOCStr = f"{fctmp[i]}-{fctmp[i+1]}-{fctmp[i+2]}"
else:
airptOCStr += f"-{fctmp[i+1]}-{fctmp[i+2]}"
if not totalAirptOCStr:
totalAirptOCStr = f"{fctmp[i]}-{fctmp[i+1]}-{fctmp[i+2]}"
else:
totalAirptOCStr += f"-{fctmp[i+1]}-{fctmp[i+2]}"
segno += 1
totalseg += 1
p2 += 1
i += 1
for row in result:
row[13] = totalAirptOCStr
if all([
re.match(r'^\w{3}$', row[4]),
re.match(r'^\w{2}$', row[5]) or row[5] == "//",
re.match(r'^\w{3}$', row[6]),
re.match(r'^\d+\.\d+$', row[7]),
re.match(r'^\d+\.\d+$', row[8]),
re.match(r'^\w{3}$', row[9])
]):
print(row)
else:
print(f"{ERR}{ticketid},{ticketno},{fc_data}")
```
这个Python版本实现了与原Perl脚本相似的功能,并保持了处理流程的一致性。注意,在实际应用中可能还需要调整以适应具体的上下文环境和其他依赖条件。
阅读全文