2016-11-21 64 views
0

我知道我錯過了一些明顯的東西。我使用argparse來解析兩個輸入文件。當我打印變量'file1'和'file2'時,我從主函數中得到預期的輸出。然而,我試圖在子函數中使用'file1'和'file2'。我也試圖打印出新的變量(失敗)。我想要的是將命令行參數設置爲變量,然後在代碼中使用這些變量。在Python中使用open()與命令行參數

""" 
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」,「B」,「S」和「K」不打印(是我期待了很多輸出:

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

請給出[MCVE]包括你如何調用這個和會發生什麼時你做。 – jonrsharpe

+0

添加了修改。謝謝。 –

+0

您不執行'merge_csvs()'和'diff_csvs()',因此無法打印。 – furas

回答

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()方法

  • 添加文件1和file2作爲參數

  • 呼叫兩個從主函數()以外,對於每個呼叫

  • 提供文件1和file2作爲參數

上面的代碼未經測試。我只是修改你的代碼

+0

是的,這是有效的。我沒有打電話給其他功能。我還有一個下面的代碼版本,它刪除了agrs_parser(),並設置了全局解析變量,以便在代碼中稍後的merge_csvs()和diff_csvs()中使用 –

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功能,狂全局變量可用於其他功能的