我应该如何在PowerShell中执行此Python脚本

时间:2017-10-19 08:15:05

标签: python powershell

  

我已经解决了这个问题。问题与我的%PATH%

有关

我有一个使用参数的脚本。在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)

注意:脚本和文件位于同一位置。

完全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()

1 个答案:

答案 0 :(得分:0)

通常在powershell中执行python脚本,如.\script.py有两个要求:

  1. 将python二进制文件的路径添加到%path%$env:Path = $env:Path + ";C:\Path\to\python\binaries\"
  2. 将结尾.py添加到pathtext环境变量:$env:PATHEXT += ";.PY"
  3. 后者仅用于当前的PowerShell会话。如果要将其添加到所有将来的PowerShell会话中,请将此行添加到您的powershell配置文件中(f.e. notepad $profile)。

    在您的情况下,您尝试执行的python脚本也存在问题。 def __init__(self)是类的构造函数,如:

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

    你有没有给我们完整的脚本?