标题:打印的内容显示正常,而在输出的文件中内容却是重复的并且显示的是列表最后 ...
取消只看楼主
hkcmd
Rank: 2
等 级:论坛游民
帖 子:37
专家分:46
注 册:2013-8-21
结帖率:100%
 问题点数:0 回复次数:2 
打印的内容显示正常,而在输出的文件中内容却是重复的并且显示的是列表最后一个元素?

程序代码:
# -*- 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()))
搜索更多相关主题的帖子: name 内容 打印 显示 def 
2019-10-24 15:19
hkcmd
Rank: 2
等 级:论坛游民
帖 子:37
专家分:46
注 册:2013-8-21
得分:0 
回复 2楼 fall_bernana
谢谢,我尝试了把这个引用的对象用索引来区分(j),但最后还是会修改bm的所有元素的值
程序代码:
bp = [bp] * len(A)
...
bp[j][0]['Name'] = A[i][1]
bp[j][0]['Desc'] = A[i][2]            
bp[j][0]['SN'] = str(A[i][0])            
bp[j][0]['Link'] = 'Net1@' + A[i][1]
bp[j][0]['Ad'] = str(A[i][-1])

该如何修改?
2019-10-24 17:07
hkcmd
Rank: 2
等 级:论坛游民
帖 子:37
专家分:46
注 册:2013-8-21
得分:0 
回复 4楼 fall_bernana
谢谢,已经看到效果了,但是bp的元素个数接近1000个,这样子弄程序会显得太臃肿?
2019-10-24 17:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-497419-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020960 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved