Python Forum
isolating part of elemnnt in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
isolating part of elemnnt in a list
#8
Forum help document > BBCode

This code will create a single file from the .dat files in the folder
import glob

def yield_dat_file(file_name):
    with open(file_name) as file_obj:
        next(file_obj)
        for line in file_obj:
            frame, action = line.split()
            file_number, _ = file_name.split('.')
            yield int(file_number), int(frame), action

def yield_all_dat_files():
    dat_file_names = glob.glob('*.dat')
    for file_name in sorted(dat_file_names):
        for dat_row in yield_dat_file(file_name):
            yield dat_row

def create_single_dat():
    with open('FINAL.dat', 'w') as file_obj:
        file_obj.write('MohoSwitch1\n')
        for file_number, frame, action in yield_all_dat_files():
            file_obj.write('{} {}\n'.format(file_number + frame, action))

create_single_dat()
Final.dat
MohoSwitch1
1 FV
2 L
235 AI
237 etc
565 rest
577 E
Reply


Messages In This Thread
isolating part of elemnnt in a list - by trepaning - Oct-29-2016, 04:06 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 04:31 PM
RE: isolating part of elemnnt in a list - by buran - Oct-29-2016, 04:33 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 08:49 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 09:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding List Element if Second part of the List Elements are the Same quest_ 3 2,555 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Select a part of an element of a list with Index BollerwagenIng 0 1,900 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020