打印的内容显示正常,而在输出的文件中内容却是重复的并且显示的是列表最后一个元素?
程序代码:
# -*- coding: utf-8 -*- # 存放桌面路径 path = "C:\\Users\\hkcmd\\Desktop\\" bp = [{"Link":"Net1@a","Name":"aa","Desc":"AA","SN":"49","Vl":"false","Fn":"3","Ad":"1200","RC":"1","VT":"4","KV":"1","BV":"0","ST":"50","PY":"0","Bl":"0"}] A = [ [28, 'a', 'A', 1217], [29, 'b', 'B', 1219], [30, 'c', 'C', 2201], [31, 'd', 'D', 2202], [32, 'e', 'E', 4203], [33, 'f', 'F', 4220] ] # 写入数据到指定文件 def WriteData(name, data): pfile = open(path + name,'a') pfile.write(data + "\n") pfile.close() # 返回数据坐标 def xx(x): return x - (x // 1000 - 1) * 1000 def X(): bm = [] for i in range(5): addr = xx(A[i][-1]) if addr >= 1200 and addr < 1300: bp[0]['Name'] = A[i][1] bp[0]['Desc'] = A[i][2] bp[0]['SN'] = str(A[i][0]) bp[0]['Link'] = 'Net1@' + A[i][1] bp[0]['Ad'] = str(A[i][-1]) bm.append(bp[0]) print(bp[0]) return bm WriteData("uuu.csv","\n".join("%s" %x for x in X()))