我有一个 csv 文件,例如:
cutsets
x1
x3,x5
x2
x4,x6
x5,x7
x6,x8
x7,x9
x6,x8,x10
我运行以下 Py 脚本:
import csv
# Reads Boolean expression from cutsets file
expr = []
with open("MCS_overlap.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file)
# skip the first row
next(csv_reader)
for lines in csv_reader:
expr = expr + lines + ['|']
del expr[-1]
final_expr=str(''.join(expr)).replace(",","&")
print("The Boolean expression is")
print(final_expr)
并得到输出:
The Boolean expression is
x1|x3x5|x2|x4x6|x5x7|x6x8|x7x9|x6x8x10
我希望final_expr=str(''.join(expr)).replace(",","&")在“|”括起来的任何两个变量之间得到一个“&”,例如“x4&x6”、“x6&x8&x10”。但可以看出,变量只是简单地连接起来。鉴于我无法更改输入文件的格式,如何完成插入“&”?
梦里花落0921
犯罪嫌疑人X
相关分类