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
#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
Reply
#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
#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
Reply
#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
Reply
#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
Reply
#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?
Reply
#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
Reply
#8
(Jul-16-2019, 02:16 PM)ichabod801 Wrote: I would look at our Free Python Resources page.

Thank You!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 403 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  file open "file not found error" shanoger 8 944 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Replace a text/word in docx file using Python Devan 4 2,852 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Color a table cell based on specific text Creepy 11 1,828 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 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,120 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,500 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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