2016-09-14 3 views
-1

スプレッドシートの各項目(行)のディレクトリを作成しようとしていますが、どうすればpython 3.5でそれを行うことができますか? pipとcondaを使ってパンダをインストールしようとしましたが、動作しません。visual-C++ビルドツールが必要です。ツールをインストールしても同じエラーが発生します。 。 パンダは.xlsシートの各行のディレクトリを作成する最も良い方法ですか?私は複数の.xlsファイルを持っています.xlsの各項目のディレクトリを作成する方法python

+1

組み込みのVBAを試しましたか? https://www.techonthenet.com/excel/formulas/mkdir.php –

答えて

1

あなたの質問のいくつかのポイント私にとって非常に明確ではないですが、私はいくつかのアイデアを提供しようとします。 xlrdを使用することができます(こちらのドキュメント:http://xlrd.readthedocs.io/en/latest/index.html)。

「sample.xls」というファイルがあり、その中に多数のシートがあるとします。各シートに対して、そのシートの行数として「C:\ test」(Windowsのパスと見なします)にいくつでもフォルダを作成します。また、シートの名前に続いてプログレッシブ数字 を使用して、そのようなフォルダに名前を付けることを前提とします(実際のニーズに合わせて簡単にコードを編集できます)。

import os 
from xlrd import open_workbook 

parentPath = r"C:\test" 
xlsFile = open_workbook('sample.xls') # Open the xls file 
for sheetName in xlsFile.sheet_names(): # Loop over the sheets inside the xls file 
    i = 1 # Initialize the index to be used in folder names 
    for row in xlsFile.sheet_by_name(sheetName).col(0): # Select the first column and loop over the rows 
     childPath = ''.join([sheetName, '_', str(i)]) 
     newPath = os.path.join(parentPath,childPath) 
     if not os.path.exists(newPath): # Make sure the path does not exist 
      os.makedirs(newPath) 
     i += 1 

PS:私は、CSVファイルを使用すると少し楽になることに同意します。

EDIT:

次のソリューションを使用すると、XLSファイル内の各シートに(空でない)細胞の数と同じ数のフォルダを作成したいという仮定に基づいて、各フォルダを持っているということです'sheetName_rowi_colj'の形式の名前 ここで、iとjは2つのインデックスであり、シート内のセルの位置に関連しています。

import os 
from xlrd import open_workbook 

parentPath = r"C:\test" 
xlsFile = open_workbook('sample.xls',ragged_rows=True) # Open the xls file 
for sheetName in xlsFile.sheet_names(): # Loop over the sheets inside the xls file 
    for rowIdx in range(xlsFile.sheet_by_name(sheetName).nrows): # Loop over the rows 
     for colIdx in range(xlsFile.sheet_by_name(sheetName).row_len(rowIdx)): # Loop over the columns for each row 
      if xlsFile.sheet_by_name(sheetName).cell_value(rowIdx,colIdx) != '': # Check if the cell is empty 
       childPath = ''.join([sheetName, '_row', str(rowIdx+1), '_col', str(colIdx+1)]) # +1 because indices start from zero 
       newPath = os.path.join(parentPath,childPath) 
       if not os.path.exists(newPath): # Make sure the path does not exist 
        os.makedirs(newPath) 

xlsファイルの数が多い場合は、それらのファイルをループするだけです。

0

ファイルをCSV形式で保存すると、この作業がはるかに簡単になります。これを試してみてください:

import csv, sys, os 

folder_list = [] 
with open('folders.csv', 'rb') as f: 
    reader = csv.reader(f) 
    for row in reader: 
     for item in row: 
      if item != None: 
       folder_list.append(item) 
       print item 

for folder in folder_list: 
    try: 
     os.makedirs(folder) 
    except WindowsError as e: 
     pass 
関連する問題