2017-03-22 135 views
1

下午好,複製文件 - 環蟒蛇

我這裏有一些代碼,是爲了從源文件夾複製約200 CSV年代進入基於「部門」,他們屬於目標文件夾。他們所屬的部門通過「符號列表」進行標識,其中包含csvs和一系列代號。該代碼適用於大部分部分,不同之處在於它將符號列表中最後csv中的代號列表複製並將所有這些CSV複製到所有目標文件夾中。基本上我需要以某種方式將第一個for循環與下面的3 for循環組合起來,但是我很難做到這一點。任何意見,不勝感激。

import os,sys,shutil 
import glob 
import pandas as pd 

source_dir = 'C:\TS' 
dest_dir = 'C:\TS\Combined\Groups\Cross Asset Class' 
#dest_dir = 'C:\TS\Combined\copytest' 
base = 'C:\TS\Combined\Groups' 

dest_dirlist = (base +'/Cross Asset Class', base+'/Bonds', base + '/Commodities', \ 
     base + '/Countries', base + '/Currencies', base + '/Industry Sectors', base + '/Segments and Styles', \ 
     base + '/Us Sectors', base + '\Volatilities') 
print(dest_dirlist) 

symbolslist = (base+'/Cross Asset Class.csv', base+'/Bonds.csv',base+'/Commodities.csv' \ 
       ,base+'/Currencies.csv', base+'/Industry Sectors.csv', base+'/Segments and Styles.csv', \ 
        base+'/US Sectors.csv') 


for file in symbolslist: 
    print(file) 
    df_symbolslist = pd.read_csv(file) 
    print(df_symbolslist) 


for ticker_file in glob.glob(os.path.join(source_dir, '*.csv*')): 
    for ticker in df_symbolslist['Ticker']: 
     print(ticker) 
     if ticker in ticker_file: 
      for path in dest_dirlist: 
       shutil.copy(ticker_file, path) 
       print(ticker + ' File Copied') 

非常感謝您的時間。

+0

'df_symbolslist = pd.read_csv(文件)'保持overwritting'df_symobolslist'。你只處理最後一個。我對這裏的格式感到困惑...... symbollist csv的外觀如何?他們如何編碼部門和符號? – tdelaney

+0

這就是符號列表csv的外觀:http://prntscr.com/en97is – user7669093

+0

這就是要複製的csvs的樣子。 http://prntscr.com/en98d2 – user7669093

回答

0

如果我正確理解問題,我認爲你應該遵循一個稍微不同的方法。您可以創建一個dict,將代號映射到它所屬的資產類別列表。然後你映射到複製文件。我不認爲熊貓可以幫助你 - 你可以使用標準的csv模塊逐行建立地圖。

我沒有測試此代碼,因爲我沒有正確的數據集,但考慮到這樣做:

import os,sys,shutil 
import glob 
import csv 
import collections 

source_dir = r'C:\TS' 
dest_dir = r'C:\TS\Combined\Groups\Cross Asset Class' 
#dest_dir = r'C:\TS\Combined\copytest' 
base = r'C:\TS\Combined\Groups' 

# asset classes of interest 
asset_classes = ('Cross Asset Class', 'Bonds', 'Commodities', 
    'Countries', 'Currencies', 'Industry Sectors', 'Segments and Styles', 
    'Us Sectors', 'Volatilities') 

# asset class directories indexed by class 
dest_dir_index = {asset_class.upper():os.path.join(base, asset_class) 
    for asset_class in asset_classes} 
print(dest_dir_index) 

# make sure destination dirs exist 
for dir_name in dest_dir_index.values(): 
    if not os.path.isdir(dir_name): 
     os.mkdir(dir_name) 

# dict that creates key:[classes...] item when first accessed, used to keep 
# list of asset classes for each ticker symbol. 
ticker_to_class_index = collections.defaultdict(list) 

for asset_class in asset_classes: 
    symbolcsv = "{}.csv".format(os.path.join(base, asset_class)) 
    print(symbolcsv) 
    with open(symbolcsv, newline='') as fp: 
     reader = csv.reader(fp) 
     next(fp) # skip header 
     for sectors, ticker in reader: 
      ticker_to_class_index[ticker.upper()].append(asset_class) 

# split ticker out of csv filenames and copy file to all asset classes 
# mapped for that ticker. 
for ticker_file in glob.glob(os.path.join(source_dir, '*.csv*')): 
    ticker = os.path.splitext(os.path.basename(ticker_file)).upper() 
    print(ticker) 
    for asset_class in ticker_to_class_map[ticker.upper()]: 
     dest_dir = dest_dir_index[asset_class] 
     shutil.copy(ticker_file, dest_dir) 
     print("{} copied to {}".format(ticker_file, dest_dir)) 
+0

非常感謝tdelaney。讓我試試看。此外,這裏是一個保管箱鏈接到我的csvs的樣子.https://www.dropbox.com/s/iqbt3pa6qiz70vg/Cross%20Asset%20Class.csv?dl = 0 – user7669093

+0

他是我想要的文件類型複製。 https://www.dropbox.com/s/vusdpn8oizcda82/XLY.csv?dl=0 – user7669093

+0

給它一個鏡頭,但它似乎並不喜歡49或51行中的「上層」。給我一個元組錯誤: ticker = os.path.splitext(os.path.basename(ticker_file))。upper() AttributeError:'元組'對象沒有屬性'upper' – user7669093