variables = dict() #Create a dictionary to store the split lines
with open("input.txt") as i_file: #Open the file
for line in i_file:
if line == "\n" or line.find("=") == -1: #Skip blank lines and lines with no equals sign
continue
name, value = line.strip("\n").split(" = ") #Split lines into two parts based on the "=" sign
if name not in variables:
variables[name] = list()
variables[name].append(int(value))
with open("output.txt", "w") as o_file: #Can be changed to whatever file you want to write to
for item in variables:
temp = str(variables[item])
temp = temp.replace("[","{").replace("]","}").replace(" ", "")
o_file.write(item + " = " + temp +"\n")
这将对您有用:
相关问题 更多 >
编程相关推荐