简体   繁体   中英

Split one txt to several txt files with particular name in Python

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)

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]))

You can store all the months and corresponding data (first date of month, list of currencies) as key, value pairs in a dictionary. Then, you can write the dictionary to files, whose names will be based on the first date of the month and content on remaining values in the list.

dict = {}
with open("Data.txt", 'r', encoding="utf-8") as fp:
    for line in fp:
        tokens = line.split()
        date = tokens[0]
        names = tokens[1:]
        list_of_names = dict.get(date[3:], [])   # get existing list of names or new list if key does not exist.
        if not list_of_names:
           list_of_names.append(date)
           list_of_names.extend(names)
        else:
           list_of_names.extend(names)
        dict[date[3:]] = list_of_names

for date, list_of_names in dict.iteritems():
  with open(list_of_names[0] +'.txt', 'w') as f:
     f.write('\n'.join(list_of_names[1:]))     # write all names in one go

You could use Python's groupby function to group lines with a matching month and year from the file. The lambda function uses split() twice to extract the month and year, first by splitting each line by the space, and then by taking the first entry and splitting it by a . to get the 3 date components and returning the month/ years parts.

Then for each entry in the group you can then create a file based on the filename of the first entry in this group as follows:

from itertools import groupby


with open('data.txt') as f_input:
    for k, g in groupby(f_input, lambda x: x.split()[0].split('.')[1:]):
        entries = list(g)
        with open(entries[0].split()[0], 'w') as f_output:
            for line in entries:
                print line
                f_output.write(line.split(' ', 1)[1].lstrip())

This would result in the following 4 files being created:

24.03.2016
18.04.2016
17.05.2016                    
16.06.2016

The 24.03.2016 file for example would contain:

Peso
Ruble

Note, this assumes your entries in data.txt are in chronological order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM