在Python中使用open()和Command Line Argument

时间:2016-11-21 15:21:04

标签: python arguments argparse

我知道我错过了一些明显的东西。我使用argparse来解析两个输入文件。当我打印变量' file1'时,我得到主函数的预期输出。和' file2'

但是我尝试使用' file1'和' file2'在子功能。我还尝试打印出新的变量(失败)。我想要的是将命令行参数设置为变量,然后在代码中使用这些变量。

"""
Created on Fri Oct 21 12:02:34 2016

@author: jsklein
"""
import pandas as pd
import csv
import argparse

# Parse command line arguments and set them to variables to be used later
def main():
    parser = argparse.ArgumentParser(description='Compares Two CSV files for matches and differences indexed on a column')

    parser.add_argument("-i", help="Name of first CSV import file", action="store", dest="infile1", required="yes")
    parser.add_argument("-I", help="Name of second CSV import file", action="store", dest="infile2", required="yes")

    args = parser.parse_args()    
    file1 = args.infile1
    file2 = args.infile2
    print(file1)
    print(file2)
# Define Compare funtion that joins on specified column

    def merge_csvs():
        a = pd.read_csv(file1)
        b = pd.read_csv(file2)
        print(a)
        print(b)

        merged = b.merge(a, on='SWREV')
        merged.to_csv("merged_results.csv", index=False) 

 # Define Diff function that diffs on specified column

    def diff_csvs():
        s = open(file1, 'r')
        k = open(file2, 'r')
        print(s)
        print(k)

        checkS = csv.reader(s)
        checkK = csv.reader(k)

        output1 =  [row for row in checkS if row not in checkK]
        output2 =  [row for row in checkK if row not in checkS]

        with open("A_notin_B.csv", "w") as f:
            writer = csv.writer(f)
            writer.writerows(output1)

        with open("B_notin_A.csv", "w") as l:
            writer = csv.writer(l)
            writer.writerows(output2)

# Main Function that Calls all the other functions


main()

以下是运行代码的示例,请注意其他变量' a''''''' k& #39;不打印(是的,我期待很多输出:

$ python csv_compare.py -i csv1.csv -I csv2.csv
csv1.csv
csv2.csv

2 个答案:

答案 0 :(得分:1)

我不确定,但也许这有帮助(如果这是你想要做的):

import pandas as pd
import csv
import argparse

# Parse command line arguments and set them to variables to be used later
def main():
    parser = argparse.ArgumentParser(description='Compares Two CSV files for matches and differences indexed on a column')

    parser.add_argument("-i", help="Name of first CSV import file", action="store", dest="infile1", required="yes")
    parser.add_argument("-I", help="Name of second CSV import file", action="store", dest="infile2", required="yes")

    args = parser.parse_args()    
    file1 = args.infile1
    file2 = args.infile2
    print(file1)
    print(file2)

    # explicitly call the other functions
    merge_csvs(file1,file2)
    diff_csvs(file1,file2)

# Define Compare funtion that joins on specified column
def merge_csvs(file1,file2):
    a = pd.read_csv(file1)
    b = pd.read_csv(file2)
    print(a)
    print(b)

    merged = b.merge(a, on='SWREV')
    merged.to_csv("merged_results.csv", index=False) 

# Define Diff function that diffs on specified column
def diff_csvs(file1,file2):
    s = open(file1, 'r')
    k = open(file2, 'r')
    print(s)
    print(k)

    checkS = csv.reader(s)
    checkK = csv.reader(k)

    output1 =  [row for row in checkS if row not in checkK]
    output2 =  [row for row in checkK if row not in checkS]

    with open("A_notin_B.csv", "w") as f:
        writer = csv.writer(f)
        writer.writerows(output1)

    with open("B_notin_A.csv", "w") as l:
        writer = csv.writer(l)
        writer.writerows(output2)

# Main Function that Calls all the other functions
main()

基本上我做的是:

  • 在main()方法之外定义函数

  • 将file1和file2添加为参数

  • 从main()调用两个函数,提供file1和file2作为每个调用的参数

上面发布的代码未经测试。我刚编辑了你的代码

答案 1 :(得分:0)

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 21 12:02:34 2016

@author: jsklein
"""
import pandas as pd
import csv
import argparse

# Parse command line arguments and set them to variables to be used later
parser = argparse.ArgumentParser(description='Compares Two CSV files for       matches and differences indexed on a column')

parser.add_argument("-i", help="Name of first CSV import file", action="store", dest="infile1", required="yes")
parser.add_argument("-I", help="Name of second CSV import file", action="store", dest="infile2", required="yes")

args = parser.parse_args()    
file1 = args.infile1
file2 = args.infile2
print(file1)
print(file2)

# Define Compare funtion that joins on specified column

def merge_csvs():
    a = pd.read_csv(file1)
    b = pd.read_csv(file2)
    print(a)
    print(b)
    merged = b.merge(a, on='SWREV')
    merged.to_csv("merged_results.csv", index=False) 

# Define Diff fuction that diffs on specified column

def diff_csvs():

    s = open(file1, 'r')
    k = open(file2, 'r')
    print(s)
    print(k)
    checkS = csv.reader(s)
    checkK = csv.reader(k)

    output1 =  [row for row in checkS if row not in checkK]
    output2 =  [row for row in checkK if row not in checkS]

    with open("A_notin_B.csv", "w") as f:
        writer = csv.writer(f)
        writer.writerows(output1)

    with open("B_notin_A.csv", "w") as l:
        writer = csv.writer(l)
        writer.writerows(output2)

# Main Function that Calls all the other functions
def main():
    merge_csvs()
    diff_csvs()

main()

所以我摆脱了arg_parser函数,它使其他函数可用的全局变量疯狂

相关问题