Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python循环只返回最后一个值_Python_Python 2.7 - Fatal编程技术网

Python循环只返回最后一个值

Python循环只返回最后一个值,python,python-2.7,Python,Python 2.7,我的英语可能不好,所以我很抱歉 嗨。我对python循环有一些问题。我的脚本统计两个日期之间的所有工作日,并在每一天将所有文本从x.txt复制到textfile.txt,在文本开头插入日期。x是从1到n的几个文本文件。例如,开始日期为1,12017,结束日期为5,12017。1.txt包含aaa,2.txt为bbb,3.txt为ccc等。输出文件应为: 2017-01-02 (because 1 January is Sunday which is weekend) aaa 2017-01-

我的英语可能不好,所以我很抱歉

嗨。我对python循环有一些问题。我的脚本统计两个日期之间的所有工作日,并在每一天将所有文本从x.txt复制到textfile.txt,在文本开头插入日期。x是从1到n的几个文本文件。例如,开始日期为1,12017,结束日期为5,12017。1.txt包含aaa,2.txt为bbb,3.txt为ccc等。输出文件应为:

2017-01-02 (because 1 January is Sunday which is weekend)

aaa

2017-01-03

bbb

2017-01-04

ccc

2017-01-05

ddd
但是看起来

2017-01-05

aaa

2017-01-05

bbb

2017-01-05

ccc (only 3 files are processed, should be 4 and only last date is inserted everywhere)
我添加了
printn
试图跟踪问题所在,在
之后,对于范围(1,n)内的x:
它给了我

2
3
3
4
4
4
这是我的密码:

# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import timedelta
import shutil
import os


def dc(d1, m1, y1, d2, m2, y2):
    start = datetime(y1, m1, d1)
    end = datetime(y2, m2, d2)
    delta = timedelta(days=1)
    d = start
    n = 1
    weekend = ([5, 6])
    while d <= end:
        if d.weekday() not in weekend:
            with open('textfile.txt','wb') as destination:
                for x in range (1, n):
                    print n
                    with open(str(x) + '.txt','rb') as source:
                        destination.write(str(d)[:10])
                        destination.write(os.linesep)
                        shutil.copyfileobj(source, destination)
                        destination.write(os.linesep*2)
        n += 1
        d += delta

dc(1,1,2017,5,1,2017)
#-*-编码:utf-8-*-
从日期时间导入日期时间
从日期时间导入时间增量
进口舒蒂尔
导入操作系统
def dc(d1、m1、y1、d2、m2、y2):
开始=日期时间(y1、m1、d1)
结束=日期时间(y2、m2、d2)
增量=时间增量(天数=1)
d=开始
n=1
周末=([5,6])

当d时,您应该使用while循环或for循环

x = 0
with open('textfile.txt','wb') as destination:
    while d <= end:
        if d.weekday() not in weekend:
            with open(str(x) + '.txt','rb') as source:
                #<Do your stuff>
            #Next line will not skip any file
            x += 1
        #if you want to skip files as you move, use next line (remove '#') and remove the above line.
        #x+=1
        delta+=1
x=0
以open('textfile.txt','wb')作为目标:

当d时,您应该使用while循环或for循环

x = 0
with open('textfile.txt','wb') as destination:
    while d <= end:
        if d.weekday() not in weekend:
            with open(str(x) + '.txt','rb') as source:
                #<Do your stuff>
            #Next line will not skip any file
            x += 1
        #if you want to skip files as you move, use next line (remove '#') and remove the above line.
        #x+=1
        delta+=1
x=0
以open('textfile.txt','wb')作为目标:

而d请修正代码中的缩进。现在不正确。请修复代码中的缩进。现在不对,谢谢!我会试试的,请修复你的压痕。很遗憾,我做不到。也许我需要以另一种方式重写代码嗨,Alex,我确信你仍然需要在while函数之前包含with open函数。。。它有点像你想要的,除了打印像这样的东西。。。我忘了提到这个还在进行中。我不想把这个问题搁置太久…谢谢!我会试试的,请修复你的压痕。很遗憾,我做不到。也许我需要以另一种方式重写代码嗨,Alex,我确信你仍然需要在while函数之前包含with open函数。。。它有点像你想要的,除了打印像这样的东西。。。我忘了提到这个还在进行中。我不想把这个问题搁置太久。。。