Split one txt to several txt files with particular name in Python
Question: I have a txt file which looks like:

    
    
    24.03.2016  Peso
    27.03.2016  Ruble
    18.04.2016  Euro 
    17.05.2016  Dollar
    16.06.2016  Frank
    

I need to split it in different files, and the name of a new file should be
the date, and the stuffing of this file - the rest. For example - the name is
**18.04.2016** and inside the file is **Euro**.

But if its the same month (like 03.2016 here), i need to put it all in one
file, with a name of the first date of this month. For example - the name is
**24.03.2016** , and inside is **Peso /n Ruble**.

How I can do that? Now i'm only on the step of reading my file line by line:

    
    
    with open("Data.txt", 'r', encoding="utf-8") as fp:
        for line in fp:
            read (line)
    


Answer: Something like this:

    
    
    #!python3
    import collections
    
    seen = collections.defaultdict(list)
    
    with open("Data.txt", 'r', encoding="utf-8") as fp:
        for line in fp:
            line = line.strip()
            if not line:
                continue
    
            date,currency = line.split()
            month = date[3:]
    
            seen[month].append((date,currency))
    
    for month in seen.keys():
        with open(seen[month][0][0], 'w') as outfile:
            print(file=outfile,
                "\n".join(currency for date,currency in seen[month]))
    

