2016-11-04 50 views
0

我想添加新行到列表,但它似乎只有最后一行复制到列表中多次有一个新行生成。 所以我做了一个测试脚本来证明我的问题。 我想要的是添加尽可能多的行到原始列表,因为行中有NIGHTS,并添加一天到原始日期,如果有7晚,我想有7行只有不同的日期。从那我生成一个文本文件给我发邮件,这是一种日常报告,所以我知道在我们的B & B中打开或关闭热水锅炉的内容Python添加新行列表重复只有最后一个

这是我在Python中的测试脚本

#!/usr/bin/env python 
# -*- coding: UTF-8 -*- 

from datetime import datetime, timedelta 

NIGHTS = 12 # number of nights a guest stays @ our B&B 
CHECKIN = 0 # date the guest checks in 
aList = [] 

""" 
contents test agenda.dat 
    check in       nights 
    V         V 
"2016-10-27,1,*,K4,A1,*,D4,*,PD,PF,0,*,7,TEST4,remark" 
"2016-10-28,1,*,K4,A1,*,D4,*,PD,PF,0,*,1,TEST1,remark" 
"2016-10-29,1,*,K4,A1,*,D4,*,PD,PF,0,*,1,TEST2,remark" 
"2016-10-30,1,*,K4,A1,*,D4,*,PD,PF,0,*,2,TEST3,remark" 

copy past this into agenda.dat and you have your file 

""" 

# 
# --------- convert date --------- # 
def date(cDate=None, format='%Y-%m-%d'): 
    if not cDate: 
     dDate = datetime.today().date() 
    else: 
     dDate = datetime.strptime(cDate, '%Y-%m-%d') 

    return dDate 

# 
# --------- get contents agenda -------- # 
# 
def read_agenda(): 

    aBooking=[] 

    with open("agenda.dat", "r") as f: 
     next(f) # skip header line 

     for line in f: 
      line = line.strip() 
      aBooking.append(line.split(",")) 

    return aBooking 


aList = read_agenda() 

# checking out contents of aList 
###### 
for x in aList: 
    print x 

print "=============" 
# it checks out 

# thought using an empty list would solve the problem, not 
bList = [] 

newline = [] # just declaring everything would help? 
n = 0   # not 

####### 
for x in aList: 
    n = int(float(x[NIGHTS])) 
    newline = x 

    # the first agenda-data line contains '7' so n = 7 

    while n > 1: 
     cDatum = date(newline[CHECKIN]).date() + timedelta(days=1) 
     newline[CHECKIN] = "{:%Y-%m-%d}".format(cDatum) 
     newline[NIGHTS] = str(1) 

     # just to check if the conversion went ok, yes 
     print newline 
     # output: 
     # ['2016-10-28', '1', '*', 'K4', 'A1', '*', 'D4', '*', 'PD', 
     # 'PF', '0', '*', '1', 'TEST1', 'remark'] 

     # and now it happens adding a line to this aray ends in 
     # replicating n lines with the same contents 
     # this should not be, a new line should contain a new date 
     bList.append(newline) 
     n -=1 


################ 
print "==== bList =========" 
for y in bList: 
    print y 
# this prints the same line 7 times! 

print "=== aList ==========" 
for x in aList: 
    print x 
# and yes the original data is still intact except for the 7 this turned 
# into 1 as desired 

问题: 我该怎么办错在这里 我应该得到7个系与日递增的第一条记录 和2号线的最后一个。

回答

0

您的内循环while n > 1:不断修改相同的newline对象并将其附加到bList。所以你不会在bList中得到7个不同的newline对象,你会得到7个引用相同的newline对象。

您可以修复的摆脱那个

newline = x 

线while n > 1:之前,改变循环的开始

while n > 1: 
    newline = x[:] 

这个问题,将创建一个新的newline对象(从x复制)在循环的每次迭代中。

如果x是字符串列表,此解决方案将工作。但是,如果x实际上是列表的列表,那么您可能也必须创建这些内部列表的副本。

+0

谢谢,但那并没有解决它。而不是6 x最后一天它现在复制第一次dat 6次。 这就是我的一位朋友带来的解决方案。因此,在循环内代替新行= x {:} a = [0] * 15#在范围(0,15)中为z填充0的 :#copy record a [z] = x [z] 它可能不是很蟒蛇,但它解决了这个问题。 我们认为newline = x可能是指向原始记录o.s.l.t的指针。 – LMQR15

+0

对不起,@ LMQR15。显然,我无法测试我建议的更改,因为您没有发布[mcve]。顺便说一句,如果'x [z]'是一个字符串或int,那么'a [z] = x [z]'将起作用,但如果'x [z]'是一个列表,它不会复制对象,它只是为'x [z]'命名的同一个对象创建'a [z]'另一个名称。您可能会发现这篇文章有用:[关于Python名称和值的事实和神话](http://nedbatchelder.com/text/names.html),这是由SO老将Ned Batchelder编写的。 –

+0

如果你用上面的注释创建了agenda.dat,那么该示例正在工作,我只是试图确认。 感谢您的链接和意见,它真的有助于了解哪里出了问题。我只是一个初学者,但非常有趣! – LMQR15

相关问题