Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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中迭代特定的csv行将输出一个空白文件_Python_Csv_Itertools - Fatal编程技术网

在python中迭代特定的csv行将输出一个空白文件

在python中迭代特定的csv行将输出一个空白文件,python,csv,itertools,Python,Csv,Itertools,python新手-我正在尝试格式化一组我发送的非常粗略的csv,以便我可以将它们放入一个漂亮的postgres表中进行查询和分析。为了做到这一点,我首先用CSV.Wrror清理它们,删除每个行的空白行和双引号。我的代码是这样的: import os import csv import glob from itertools import islice files = glob.glob('/Users/foo/bar/*.csv') # Loop through all of the csv

python新手-我正在尝试格式化一组我发送的非常粗略的csv,以便我可以将它们放入一个漂亮的postgres表中进行查询和分析。为了做到这一点,我首先用CSV.Wrror清理它们,删除每个行的空白行和双引号。我的代码是这样的:

import os
import csv
import glob
from itertools import islice

files = glob.glob('/Users/foo/bar/*.csv')

# Loop through all of the csv's  
for file in files:
    # Get the filename from the path
    outfile = os.path.basename(file)

    with open(file, 'rb') as inp, open('/Users/foo/baz/' + outfile, 'wb') as out:

        reader = csv.reader(inp)
        writer = csv.writer(out)
        for row in reader:
            if row:
                writer.writerow(row)
        out.close() 
它工作得非常好,完全符合我的要求。输出csv看起来很棒。接下来,我尝试从新清理的csv文件的开头和结尾切掉一定数量的包含完全不必要垃圾的行(省略前8行和最后2行)。出于我确实无法确定的原因,这部分代码的csv输出(缩进与前面的“with”块相同)完全为空:

with open('/Users/foo/baz/' + outfile, 'rb') as inp2, open('/Users/foo/qux/' + outfile, 'wb') as out2:
    writer2 = csv.writer(out2)
    reader2 = csv.reader(inp2)
    row_count = sum(1 for row in reader2)
    last_line_index = row_count - 3 
    for row in islice(reader2, 7, last_line_index):
            writer2.writerow(row)
    out2.close()
我知道,由于我的“with”用法,每个块末尾的close()是多余的-我在查看后尝试了这种方法。我还尝试将第二个“with”块放入另一个文件中,并在运行第一个“with”块后运行该文件,但仍然无效。非常感谢你的帮助

此外,以下是整个文件:

import os
import csv
import glob
from itertools import islice

files = glob.glob('/Users/foo/bar/*.csv')

# Loop through all of the csv's  
for file in files:
    # Get the filename from the path
    outfile = os.path.basename(file)

    with open(file, 'rb') as inp, open('/Users/foo/baz/' + outfile, 'wb') as out:

        reader = csv.reader(inp)
        writer = csv.writer(out)
        for row in reader:
            if row:
                writer.writerow(row)
        out.close() 

    with open('/Users/foo/baz/' + outfile, 'rb') as inp2, open('/Users/foo/qux/' + outfile, 'wb') as out2:
        writer2 = csv.writer(out2)
        reader2 = csv.reader(inp2)
        row_count = sum(1 for row in reader2)
        last_line_index = row_count - 3 
        for row in islice(reader2, 7, last_line_index):
                writer2.writerow(row)
        out2.close()
谢谢

有罪的一方是

row_count = sum(1 for row in reader2)
它从
reader2
读取所有数据;现在,当您尝试
为islice中的行(reader2,7,last\u line\u index)
获取任何数据时

此外,您可能正在读取大量的空行,因为您以二进制方式打开文件;取而代之的是

with open('file.csv', newline='') as inf:
    rd = csv.reader(inf)

您可以像这样快速修复代码(正如@Hugh Bothwell所说,我用问题注释了这行代码,您已经从变量
reader2
读取了所有数据):


这才是问题所在!加上一个快速!你太快了!。。。。。啊哈!我不知道这是一次交易!非常感谢您的快速响应!我很感激,伙计!您的解决方案也很完美,Hugh刚刚比我的收件箱快了一点:)
import os
import csv
import glob
from itertools import islice

files = glob.glob('/Users/foo/bar/*.csv')

# Loop through all of the csv's  
for file in files:
    # Get the filename from the path
    outfile = os.path.basename(file)

    with open(file, 'rb') as inp, open('/Users/foo/baz/' + outfile, 'wb') as out:

        reader = csv.reader(inp)
        writer = csv.writer(out)
        for row in reader:
            if row:
                writer.writerow(row)
        out.close() 

    with open('/Users/foo/baz/' + outfile, 'rb') as inp2, open('/Users/foo/qux/' + outfile, 'wb') as out2:
            writer2 = csv.writer(out2)
            reader2 = csv.reader(inp2)
            row_count = sum(1 for row in csv.reader(inp2)) #here you separately count the amount of rows without read the variable reader2
            last_line_index = row_count - 3 
            for row in islice(reader2, 7, last_line_index):
                    writer2.writerow(row)
            out2.close()