代码之家  ›  专栏  ›  技术社区  ›  Cina

使用Pipenv搜索Facebook广告的Python脚本

  •  0
  • Cina  · 技术社区  · 4 年前

    https://github.com/pandringa/fb-ads-tool

    我将感谢您提供的任何反馈!

    import csv
    import re
    import argparse
    import dateparser
    In [8]:
    
    parser = argparse.ArgumentParser(prog='Aggregates search results by key')
    parser.add_argument('file')
    parser.add_argument('-k', '--key')
    parser.add_argument('-o', '--out')
    Out[8]:
    _StoreAction(option_strings=['-o', '--out'], dest='out', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
    In [9]:
    
    args = parser.parse_args()
    infile = csv.DictReader(open(args.file))
    usage: Aggregates search results by key [-h] [-k KEY] [-o OUT] file
    Aggregates search results by key: error: unrecognized arguments: -f
    An exception has occurred, use %tb to see the full traceback.
    
    SystemExit: 2
    
    
    c:\users\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py:3339: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
      warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
    In [10]:
    
    rowset = {}
    real_rowset = set()
    In [11]:
    
    out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
    out.writeheader()
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-11-4d5c0f933f42> in <module>
    ----> 1 out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
          2 out.writeheader()
    
    NameError: name 'args' is not defined
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   kareem_emad    4 年前

    你在这里面临的错误与 Pipenv ,它与您的代码流有关,您可能使用 args

    我更喜欢在我用你的脚本编写的脚本中运行这样的代码

    import argparse
    import csv
    
    parser = argparse.ArgumentParser(prog='Aggregates search results by key')
    parser.add_argument('file')
    parser.add_argument('-k', '--key')
    parser.add_argument('-o', '--out')
    
    args = parser.parse_args()
    infile = csv.DictReader(open(args.file))
    
    
    rowset = {}
    real_rowset = set()
    
    out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
    out.writeheader()
    

    用命令运行它

    python your_code.py --k key --o ./file.csv  ./sample.csv
    
    

    如果你想和 Jupyter ipython 我不认为这是您的最终目标,但您的代码现在所做的,希望这有助于并准备提供更多相关的支持 fbads sdk pipenv