2017-10-19 62 views
-1

I've solved the problem. The problem is related my %PATH%我應該如何在PowerShell中執行此Python腳本

我有一個腳本可以與參數一起使用。在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)

注意:腳本和文件位於同一位置。

Full Scritp;

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%_環境變量中的其中一個文件夾中。如果不是這種情況,請按照完整路徑指定它,例如(因爲我不知道你在哪裏安裝_Python_)'「C:\ Python \ 3.5 \ python.exe」。\ dsrf2csv.py testdata.tsv.gz' (或將該文件夾添加到_%PATH%_)。 – CristiFati

+0

您預期會發生什麼可觀察的行爲,爲什麼?你的腳本不會做任何事情。 – Goyo

+0

在第一個腳本行('XX'沒有色情,與您使用的Python版本相關)中編寫測試腳本,檢查'哪個python版本是傳入的?或者輸入'#!「c:\ Program Files \ pythonXX \ python.exe」 – dsgdfg

回答

0

一般在PowerShell中執行python腳本這樣.\script.py有兩個要求:

  1. 添加路徑蟒蛇二進制文件到您的%path%$env:Path = $env:Path + ";C:\Path\to\python\binaries\"
  2. 添加結束.py到pathtext環境變量:$env:PATHEXT += ";.PY"

後者將僅在當前的PowerShell會話中使用。如果您想將其添加到所有未來的PowerShell會話中,請將此行添加到PowerShell配置文件(f.e.記事本$profile)。

在你的情況下,你正試圖執行的python腳本也有問題。 def __init__(self)是一個類的構造函數,如:

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

你給我們提供了完整的腳本嗎?

+0

我已經添加了完整的腳本。 –