2017-10-19 6 views
-1

I've solved the problem. The problem is related my %PATH%このPythonスクリプトをpowershellでどのように実行するのですか

私は引数で動作するスクリプトを持っています。 PowerShellでは、私はあなたが以下で見ることができるコマンドを試しました。

.\dsrf2csv.py C:\Python27\a\DSR_testdata.tsv.gz 

そしてまた、あなたは、以下のスクリプトを見ることができ、

def __init__(self, dsrf2csv_arg): 
    self.dsrf_filename = dsrf2csv_arg 
    dsrf_path, filename = os.path.split(self.dsrf_filename) 
    self.report_outfilename = os.path.join(dsrf_path, filename.replace('DSR', 'Report').replace('tsv', 'csv')) 
    self.summary_outfilename = os.path.join(dsrf_path, filename.replace('DSR', 'Summary').replace('tsv.gz', 'csv')) 

しかし、私はすべてのアクションは何もあり、このスクリプトを実行しないようにしてみてください。このスクリプトをファイルでどのように実行すればよいですか? (例:testdata.tsv.gz)

注:スクリプトとファイルは同じ場所にあります。

フルスクリプト;一般的に

import argparse 
import atexit 
import collections 
import csv 
import gzip 
import os 


SKIP_ROWS = ['HEAD', '#HEAD', '#SY02', '#SY03', '#AS01', '#MW01', '#RU01', 
      '#SU03', '#LI01', '#FOOT'] 

REPORT_HEAD = ['Asset_ID', 'Asset_Title', 'Asset_Artist', 'Asset_ISRC', 
       'MW_Asset_ID', 'MW_Title', 'MW_ISWC', 'MW_Custom_ID', 
       'MW_Writers', 'Views', 'Owner_name', 'Ownership_Claim', 
       'Gross_Revenue', 'Amount_Payable', 'Video_IDs', 'Video_views'] 

SUMMARY_HEAD = ['SummaryRecordId', 'DistributionChannel', 
       'DistributionChannelDPID', 'CommercialModel', 'UseType', 
       'Territory', 'ServiceDescription', 'Usages', 'Users', 
       'Currency', 'NetRevenue', 'RightsController', 
       'RightsControllerPartyId', 'AllocatedUsages', 'AmountPayable', 
       'AllocatedNetRevenue'] 


class DsrfConverter(object): 
    """Converts DSRF 3.0 to YouTube CSV.""" 

    def __init__(self, dsrf2csv_arg): 
    """ Creating output file names """ 


    self.dsrf_filename = dsrf2csv_arg 
    dsrf_path, filename = os.path.split(self.dsrf_filename) 
    print(dsrf_filename) 
    input("Press Enter to continue...") 
    self.report_outfilename = os.path.join(dsrf_path, filename.replace(
     'DSR', 'Report').replace('tsv', 'csv')) 
    self.summary_outfilename = os.path.join(dsrf_path, filename.replace(
     'DSR', 'Summary').replace('tsv.gz', 'csv')) 

    def parse_blocks(self, reader): 
    """Generator for parsing all the blocks from the file. 

    Args: 
     reader: the handler of the input file 

    Yields: 
     block_lines: A full block as a list of rows. 
    """ 

    block_lines = [] 
    current_block = None 
    for line in reader: 

     if line[0] in SKIP_ROWS: 
     continue 

     # Exit condition 
     if line[0] == 'FOOT': 
     yield block_lines 
     raise StopIteration() 

     line_block_number = int(line[1]) 
     if current_block is None: 
     # Initialize 
     current_block = line_block_number 
     if line_block_number > current_block: 
     # End of block, yield and build a new one 
     yield block_lines 
     block_lines = [] 
     current_block = line_block_number 
     block_lines.append(line) 

    # Also return last block 
    yield block_lines 

    def process_single_block(self, block): 
    """Handles a single block in the DSR report. 

    Args: 
     block: Block as a list of lines. 

    Returns: 
     (summary_rows, report_row) tuple. 
    """ 
    views = 0 
    gross_revenue = 0 
    summary_rows = [] 
    owners_data = {} 

    # Create an ordered dictionary with a key for every column. 
    report_row_dict = collections.OrderedDict(
     [(column_name.lower(), '') for column_name in REPORT_HEAD]) 
    for line in block: 
     if line[0] == 'SY02': # Save the financial Summary 
     summary_rows.append(line[1:]) 
     continue 

     if line[0] == 'AS01': # Sound Recording information 
     report_row_dict['asset_id'] = line[3] 
     report_row_dict['asset_title'] = line[5] 
     report_row_dict['asset_artist'] = line[7] 
     report_row_dict['asset_isrc'] = line[4] 

     if line[0] == 'MW01': # Composition information 
     report_row_dict['mw_asset_id'] = line[2] 
     report_row_dict['mw_title'] = line[4] 
     report_row_dict['mw_iswc'] = line[3] 
     report_row_dict['mw_writers'] = line[6] 

     if line[0] == 'RU01': # Video level information 
     report_row_dict['video_ids'] = line[3] 
     report_row_dict['video_views'] = line[4] 

     if line[0] == 'SU03': # Usage data of Sound Recording Asset 
     # Summing up views and revenues for each sub-period 
     views += int(line[5]) 
     gross_revenue += float(line[6]) 
     report_row_dict['views'] = views 
     report_row_dict['gross_revenue'] = gross_revenue 

     if line[0] == 'LI01': # Ownership information 
     # if we already have parsed a LI01 line with that owner 
     if line[3] in owners_data: 
      # keep only the latest ownership 
      owners_data[line[3]]['ownership'] = line[6] 
      owners_data[line[3]]['amount_payable'] += float(line[9]) 
     else: 
      # need to create the entry for that owner 
      data_dict = {'custom_id': line[5], 
         'ownership': line[6], 
         'amount_payable': float(line[9])} 
      owners_data[line[3]] = data_dict 

    # get rid of owners which do not have an ownership or an amount payable 
    owners_to_write = [o for o in owners_data 
         if (owners_data[o]['ownership'] > 0 
          and owners_data[o]['amount_payable'] > 0)] 
    report_row_dict['owner_name'] = '|'.join(owners_to_write) 
    report_row_dict['mw_custom_id'] = '|'.join([owners_data[o] 
               ['custom_id'] 
               for o in owners_to_write]) 
    report_row_dict['ownership_claim'] = '|'.join([owners_data[o] 
                ['ownership'] 
                for o in owners_to_write]) 
    report_row_dict['amount_payable'] = '|'.join([str(owners_data[o] 
                 ['amount_payable']) 
                for o in owners_to_write]) 

    # Sanity check. The number of values must match the number of columns. 
    assert len(report_row_dict) == len(REPORT_HEAD), 'Row is wrong size :/' 

    return summary_rows, report_row_dict 

    def run(self): 
    finished = False 
    def removeFiles(): 
     if not finished: 
     os.unlink(self.report_outfilename) 
     os.unlink(self.summary_outfilename) 
    atexit.register(removeFiles) 
    with gzip.open(self.dsrf_filename, 'rb') as dsr_file, gzip.open(
     self.report_outfilename, 'wb') as report_file, open(
      self.summary_outfilename, 'wb') as summary_file: 
     dsr_reader = csv.reader(dsr_file, delimiter='\t') 
     report_writer = csv.writer(report_file) 
     summary_writer = csv.writer(summary_file) 

     report_writer.writerow(REPORT_HEAD) 
     summary_writer.writerow(SUMMARY_HEAD) 

     for block in self.parse_blocks(dsr_reader): 
     summary_rows, report_row = self.process_single_block(block) 
     report_writer.writerow(report_row.values()) 
     summary_writer.writerows(summary_rows) 

    finished = True 

if __name__ == '__main__': 
    arg_parser = argparse.ArgumentParser(
     description='Converts DDEX DSRF UGC profile reports to Standard CSV.') 
    required_args = arg_parser.add_argument_group('Required arguments') 
    required_args.add_argument('dsrf2csv_arg', type=str) 
    args = arg_parser.parse_args() 
    dsrf_converter = DsrfConverter(args.dsrf2csv_arg) 
    dsrf_converter.run() 
+0

_Powershell_コンソールでスクリプトフォルダに移動し、 'python。\ dsrf2csv.py testdata.tsv.gz'と入力してみてください。しかしこれは_python.exe_実行可能ファイルが_%PATH%_環境変数に存在するフォルダの1つにあるという事実に依存しています。これが当てはまらない場合は、完全パスで指定してください(例:_Python_がインストールされている場所がわからないので) '' C:\ Python \ 3.5 \ python.exe "\ dsrf2csv.py testdata.tsv.gz' (または_%PATH%_にフォルダを追加してください)。 – CristiFati

+0

どのような観察可能な行動が起こると思われますか?なぜですか?あなたのスクリプトは何もしません。 – Goyo

+0

最初のスクリプト行( 'XX'はポルノではなく、使用しているPythonのバージョンに関連しています)で' '! – dsgdfg

答えて

0

この.\script.pyでは次の2つの要件があるようにPowerShellのPythonスクリプトを実行します

  1. あなた%path%にPythonのバイナリへのパスを追加します:$env:Path = $env:Path + ";C:\Path\to\python\binaries\"
  2. をするエンディング.pyを追加します。パステキスト環境変数:$env:PATHEXT += ";.PY"

現在のPowerShellセッションでのみ使用できます。これをすべての将来のpowershellセッションに追加する場合は、この行をpowershellプロファイルに追加してください(fe.noteepad $profile)。

あなたの場合、あなたが試してみようとしているpythonスクリプトに問題があります。 def __init__(self)は、次のようなクラスのコンストラクタです。

class Foo: 
    def __init__(self): 
     print "foo" 

完全なスクリプトを教えてください。

+0

私は完全なスクリプトを追加しました。 –

関連する問題