Trying to make column based file from text file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Trying to make column based file from text file (/thread-19808.html) |
Trying to make column based file from text file - scor1pion - Jul-15-2019 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. 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? RE: Trying to make column based file from text file - ichabod801 - Jul-15-2019 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 sectionThe idea is to figure out what you need to do based on what the line looks like and perhaps what the previous line is. RE: Trying to make column based file from text file - scor1pion - Jul-15-2019 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 RE: Trying to make column based file from text file - scor1pion - Jul-16-2019 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 RE: Trying to make column based file from text file - ichabod801 - Jul-16-2019 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. RE: Trying to make column based file from text file - scor1pion - Jul-16-2019 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? RE: Trying to make column based file from text file - ichabod801 - Jul-16-2019 I would look at our Free Python Resources page. RE: Trying to make column based file from text file - scor1pion - Jul-16-2019 (Jul-16-2019, 02:16 PM)ichabod801 Wrote: I would look at our Free Python Resources page. Thank You! |