Python Forum
Trying to make column based file from text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to make column based file from text file
#2
I would have something along these lines:

rows = []
mode = 'nowhere'
for line in file:
    if line:
        if mode == 'nowhere':
            mode = 'header'
            header_data = []   # resets the header info for each new section
        elif 'Access' in line:
            if 'No users with' in line:
                continue
            mode == 'access'
        if mode == 'header':
            # pull out header information and put it into header_data
        elif mode == 'Access':
            access_type, users = line.split(':') # everything after the colon
            for user in users.split(','):
                 rows.append(header_data + [user.strip(), access_type.strip()])
    else:
        mode = 'nowhere'   # start looking for new section
The idea is to figure out what you need to do based on what the line looks like and perhaps what the previous line is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Trying to make column based file from text file - by ichabod801 - Jul-15-2019, 08:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,283 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  very newbie problem on text file zapad 2 341 Apr-12-2024, 06:50 PM
Last Post: zapad
  Copy Paste excel files based on the first letters of the file name Viento 2 535 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 536 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  file open "file not found error" shanoger 8 1,364 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Replace a text/word in docx file using Python Devan 4 3,983 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 833 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Color a table cell based on specific text Creepy 11 2,250 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  How can I change the uuid name of a file to his original file? MaddoxMB 2 1,034 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,343 Jul-07-2023, 05:44 PM
Last Post: flash77

Forum Jump:

User Panel Messages

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