4

上次我在这个网站上询问有关使用glob.glob()Python 批处理文件夹中的 csv 文件的问题时,我得到了帮助。我这次尝试使用它来转置文件夹中的所有 csv 文件。下面的脚本只处理最后一个文件并停止。我究竟做错了什么?

import csv
import os
import glob

directory = raw_input ("INPUT Folder")
output = raw_input("OUTPUT Folder:")
in_files = os.path.join(directory, '*.csv')

for in_file in glob.glob(in_files):
    with open(in_file) as input_file:
        reader = csv.reader(input_file)
        cols = []
        for row in reader:
            cols.append(row)
            filename = os.path.splitext(os.path.basename(in_file))[0] + '.csv'

with open (os.path.join(output, filename), 'wb') as output_file:
    writer = csv.writer(output_file)
    for i in range(len(max(cols, key=len))):
        writer.writerow ([(c[i] if i<len(c) else '') for c in cols])
4

4 回答 4

5

您需要缩进代码的“输出”部分,以便它在for in_file循环的每次迭代中运行一次:

import csv
import os
import glob

directory = raw_input ("INPUT Folder")
output = raw_input("OUTPUT Folder:")
in_files = os.path.join(directory, '*.csv')

for in_file in glob.glob(in_files):
    with open(in_file) as input_file:
        reader = csv.reader(input_file)
        cols = []
        for row in reader:
            cols.append(row)

    # "outdent" this code so it only needs to run once for each in_file
    filename = os.path.splitext(os.path.basename(in_file))[0] + '.csv'

    # Indent this to the same level as the rest of the "for in_file" loop!
    with open (os.path.join(output, filename), 'wb') as output_file:
        writer = csv.writer(output_file)
        for i in range(len(max(cols, key=len))):
            writer.writerow ([(c[i] if i<len(c) else '') for c in cols])

在您的版本中,该代码仅在循环完成后运行一次,for in_file因此仅输出cols该循环的最终迭代留下的数据。

我还将filename = ...语句“缩小”到该for in_file级别,因为只需为 each 执行一次in_file,而不是为 eachrow执行一次in_file

于 2013-09-16T23:02:32.073 回答
0

这是一个有效的:

不得不谷歌一个小时,但在 python33 上工作和测试

import csv
import os
import glob

directory = 'C:\Python33\csv'
output = 'C:\Python33\csv2'
in_files = os.path.join(directory, '*.csv')

for in_file in glob.glob(in_files):
    with open(in_file) as input_file:
        reader = csv.reader(input_file)
        cols = []
        for row in reader:
            cols.append(row)

    # "outdent" this code so it only needs to run once for each in_file
    filename = os.path.splitext(os.path.basename(in_file))[0] + '.csv'

    # Indent this to the same level as the rest of the "for in_file" loop!
    with open (os.path.join(output, filename), 'w') as output_file:
        writer = csv.writer(output_file)
        for i in range(len(max(cols, key=len))):
            writer.writerow ([(c[i] if i<len(c) else '') for c in cols])
于 2013-11-21T15:11:12.507 回答
0

使用pandas进行数据操作可以获得很多好处:

import os
import pandas as pd

for filename in os.listdir('.'):
    # We save an augmented filename later, 
    # so using splitext is useful for more
    # than just checking the extension.
    prefix, ext = os.path.splitext(filename)
    if ext.lower() != '.csv':
        continue
    # Load the data into a dataframe
    df = pd.DataFrame.from_csv(filename, 
                               header=None, 
                               index_col=None, 
                               parse_dates=False)
    # Transpose is easy, but you could do TONS
    # of data processing here. pandas is awesome.
    df_transposed = df.T
    # Save to a new file with an augmented name 
    df_transposed.to_csv(prefix+'_T'+ext, header=True, index=False)

如果os.walk您还需要深入研究子文件夹,则版本没有太大不同。

于 2013-09-16T23:22:48.467 回答
-1

in_files 将仅返回该格式的单个结果。尝试返回一个列表:

in_files = [f for f in os.listdir(directory) if f.endswith('.csv')]
于 2013-09-16T23:06:03.870 回答