Dynamically naming edited csv, to include part of the old csv name
Question: I am using some code to merge two csvs and sort these by two columns.
Ouputting a new csv. The input csvs are of the same name just numbered 1 & 2\.
I am repeating this code for multiple sets of data. I was wondering what the
method would be to make the code output a file name containing the first part
of the original file names.

My current code:

    
    
    import pandas as pd
    
    df1 = pd.read_csv("data csv 1\September 2013 1 UUedit1.csv", delimiter = ",")
    df2 = pd.read_csv("data csv 1\September 2013 2 UUedit2.csv", delimiter = ",")
    merged = df1.merge(df2, on="Unique Element")
    delcols = "Element_y", "number_y", "date_y", "title_y", "name_y"
    
    for delcol in delcols:
        del merged[delcol]
    
    merged.rename(columns={"name_x": "name", "rdate_x": "date", "title_x": "title", "number_x": "number", "Element_x": "Element"}, inplace = True)
    merged = merged.sort("Element").reset_index(drop=True)
    merged = merged.sort("date").reset_index(drop=True)
    merged.to_csv("MRG.csv", index=False, sep = ",")
    

So in this example both the input files are called `September
2013`"number""UUedit" I want to make my code output the file name directly as
`September 2013 MRG.csv` how can this be coded? To clarify if the two original
files were `October 2013` then the output would be `October 2013 MRG.csv` Many
thanks GTPE

## Edit

Upon running the code supplied by Christian Ternus I received the following
print and traceback:

    
    
    Usage: C:/Test.py <month> <year>
    Traceback (most recent call last):
      File "C:/Test.py", line 7, in <module>
        month, year = sys.argv[1:]
    ValueError: need more than 0 values to unpack
    

I am unsure what the second variable should be set to.  
Many thanks  
GTPE

### Edit 2

I managed to get the code to work by calling it CMD, however my attempts at
calling the script through python didn't seem to work. I tried the folowing:

    
    
    import subprocess
    p = subprocess.Popen(['python', 'RawDataSheetMergerPandasTest.py September 2013'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    print out
    


Answer: Here's how to get the name of the next month, given the name of the current
month:

    
    
    import calendar
    nextmonth = calendar.month_name[1:][(calendar.month_name[1:].index(month) + 1) % 12]
    

And here's that same logic applied to your script, with a few other
improvements :) Run this script as "`./myscript.py somemonth someyear`". It'll
output a CSV file named `nextmonth year MRG.csv`, even taking into account
localization and wrapping the year properly.

    
    
    import pandas as pd
    import calendar
    import sys
    
    if len(sys.argv) != 3:
        print "Usage: {0} <month> <year>".format(sys.argv[0])
    month, year = sys.argv[1:]
    
    if not month in calendar.month_name:
        print "Invalid month! Month must be one of:{0}".format(str(calendar.month_name))
    if not year.isdigit():
        print "Invalid year! Year must be a number."
    
    nextmonth = calendar.month_name[1:][(calendar.month_name[1:].index(month) + 1) % 12]
    
    df1 = pd.read_csv("data csv 1\{0} {1} 1 UUedit1.csv".format(month, year), delimiter = ",")
    df2 = pd.read_csv("data csv 1\{0} {1} 2 UUedit2.csv".format(month, year), delimiter = ",")
    merged = df1.merge(df2, on="Unique Element")
    delcols = "Element_y", "number_y", "date_y", "title_y", "name_y"
    
    for delcol in delcols:
        del merged[delcol]
    
    merged.rename(columns={"name_x": "name", "rdate_x": "date", "title_x": "title", "number_x": "number", "Element_x": "Element"}, inplace = True)
    merged = merged.sort("Element").reset_index(drop=True)
    merged = merged.sort("date").reset_index(drop=True)
    
    if month == calendar.month_name[-1]: year = str(int(year + 1))
    
    merged.to_csv("{0} {1} MRG.csv".format(nextmonth, year), index=False, sep = ",")
    

If you don't need the next-month feature (sounds like you actually don't),
take out these two lines:

    
    
    nextmonth = calendar.month_name[1:][(calendar.month_name[1:].index(month) + 1) % 12]
    [...]
    if month == calendar.month_name[-1]: year = str(int(year + 1))
    

and replace the last line with:

    
    
    merged.to_csv("{0} {1} MRG.csv".format(month, year), index=False, sep = ",")
    

