Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Trying to make column based file from text file
#1
Hello, I've been trying to learn python for one reason. I get a several hundred page report at work that looks like this:


RMDS1.GPCAN.* APADMIN NONE
Auditing: FAILURES(READ),SUCCESS(UPDATE)
Read Access: G132928, G202909(V), G215401, G299254, Q091665,
SYSMAN3, W80UJSX(V)
Update Access: No users with Update Access
Control Access: No users with Control Access
Alter Access: PSGPAP0(S), RMDSADM(S,O), RMDSADMN(G,O)
Access None: No users with None Access

RMDS1.SSDIPS0* APADMIN NONE
Auditing: FAIL(READ)
Read Access: CAPSADM(G), CAPSPGM(G), EDIPGM(G), G062315, G202909(V),
Q091665
Update Access: SSDIPS0(S,V)
Control Access: No users with Control Access
Alter Access: RMDSAP(G)
Access None: No users with None Access

I'd like to convert it to something that looks like this.
Report


I thought I would start by trying to clean up the file first and remove what I don't need.

So far this what I have.

invalid_chars = ['(G)', '(G,O)', '(O)', '(S)','(S,O)','(S,V)','(V)']
with open('PYTHON GDL Resources.txt' ,'r') as file:
  for line in file:
   # line = filter(lambda i: i not in invalid_chars, line)
   if 'No users with' in line:
     continue
   print(line, end='')
With the filter applied (# removed) all I get is this.
<filter object at 0x0000020893D49438><filter object at 0x0000020893D49400>

How can I get something like this but with the filter applied?
Filter
Quote
#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

Quote
#3
Hi Ichabod, Thank you for your reply.

I'm sure its just my lack of understanding as this is all very new to me still. I have been taking classes through the datacamp site...still have a couple of courses to go. I haven't come across the mode function yet. Any recommendations for a more detailed self taught course?

When I tried to run the script I got this

File "C:/Users/Keith/Desktop/PY/venv/test.py", line 16
elif mode == 'Access':
^
IndentationError: expected an indented block


file = 'PYTHON GDL Resources.txt'

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
Quote
#4
Realized I wasn't opening file thought maybe that was cause of it...but still didn't work

rows = []
mode = 'nowhere'
with open('PYTHON GDL Resources.txt', 'r') as file:
    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
Quote
#5
First of all, that was not meant to be runnable code. It was just meant to be an outline of how the code would need to be organized, to get you started. For example, line 13 (in my post) was meant to be replaced with code that does what the comment says. But it's just a comment, it doesn't count as code, which is why you are getting the error you did.

mode is not a function, it is just a variable I assigned a string to. It's there to track what sort of processing is going on for that particular line of the input file.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
Ok, thank you for your time and providing the outline. Would you be able to direct me to a good source of training material that I could use for guidance?
Quote
#7
I would look at our Free Python Resources page.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
(Jul-16-2019, 02:16 PM)ichabod801 Wrote: I would look at our Free Python Resources page.

Thank You!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 172 Dec-10-2019, 11:09 PM
Last Post: Cosmosso
  convert a .py file to .exe file vigneshboolog 12 376 Nov-29-2019, 06:15 AM
Last Post: LeanbridgeTech
  capture next block of text after finding error in file kdefilip2 6 173 Nov-27-2019, 07:36 PM
Last Post: buran
  Sorting columns from text file help nickglobal101 1 133 Nov-15-2019, 07:05 PM
Last Post: newbieAuggie2019
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 172 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  How to read text file line by line SriRajesh 1 108 Nov-05-2019, 01:51 PM
Last Post: snippsat
  Copy a File & Increment File Count xingfang 1 146 Oct-29-2019, 03:58 PM
Last Post: DeaD_EyE
  auto supply values to a python script(function) from text file metro17 4 166 Oct-26-2019, 01:25 AM
Last Post: ichabod801
  Split csv file based on column value soli004 4 358 Oct-22-2019, 05:53 AM
Last Post: soli004
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 105 Oct-21-2019, 01:01 PM
Last Post: enyrb

Forum Jump:


Users browsing this thread: 1 Guest(s)