Parsing a file

This is the place for queries that don't fit in any of the other categories.

Parsing a file

Postby macpy » Wed Jul 10, 2013 9:53 pm

Hi,

I am working on a parser and my input file is a description about bunch of things written line by line. So for instance, consider the following two lines and what the corresponding variables in python should look like.

example Line1: position = 1, 1, rotation = 90, 0, 0, mass = 120; Here I want the variables to be position = [1,1,0], rotation = [90,0,0], mass = [120]
example Line2: position = 1, 1, 2, mass = 120, rotation = 90, 0; Here I want the variables to be position = [1,1,2], rotation = [90,0,0], mass = [120]
example Line3: mass = 120, rotation = 90, 0; Here I want the variables to be position = [0,0,0], rotation = [90,0,0], mass = [120]

I know the maximum number of arguments possible for each variable. For example, in the first line above, only two numbers for position ares specified; that means that the third entry in position list is zero. So I need to handle these cases while reading in the file as well. Additionally, the text might not always be in the same order; like shown in the second line above. And finally, sometimes numbers for one variable might not exist at all; like shown in line 3. Here, the position is then read as position = [0,0,0].
How do I implement such stuff? Is there some smart way of doing this? All I have in mind is this: read a line as comma or a space delimited list of words variable and then some how use if else + len(list) to figure out whats going on. But that seems way too tedious and I feel that there might be an easier way to read such things.

Any help is highly appreciated.
Thank you
Last edited by micseydel on Thu Jul 11, 2013 2:23 am, edited 2 times in total.
Reason: Locked post.
macpy
 
Posts: 4
Joined: Mon Jul 01, 2013 5:42 pm

Re: Parsing a file

Postby ochichinyezaboombwa » Wed Jul 10, 2013 10:04 pm

No code effort from your side == no help from our side. Read this.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Parsing a file

Postby macpy » Wed Jul 10, 2013 10:55 pm

Hi,

I am at the level of thinking about this and of course I will write code when I have decided about how I am going to write this. I just wanted to know if there are libraries etc or parsers or some way to do this that I can use. This is not for a class or anything so borrowing libraries is fine. Also, I did google plenty before I decided to ask. Also, I have other things like classes for the data structures and operations where I want this data to go into after I read in the file. There is plenty of stuff that is done with the numbers that get read in and I have all of that done. Now, I need to just fill the data in. So I do have a general idea and plenty of code towards my goal but at this point, I wanted to see if someone has any suggestions on how to proceed. I am not asking for the entire thing to be done by someone else.
Thank you for the help.
macpy
 
Posts: 4
Joined: Mon Jul 01, 2013 5:42 pm

Re: Parsing a file

Postby ochichinyezaboombwa » Wed Jul 10, 2013 11:03 pm

What you need to do is trivial and doesn't require any libraries. But you've got to try or you're stuck.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Parsing a file

Postby micseydel » Thu Jul 11, 2013 2:38 am

What you've provided looks like no programmer would create that kind of input, it's not meant to be read by a computer. That said, you can still totally write code to parse it, it just won't be as elegant as an experienced programmer would prefer. Since it's not meant for a computer to read, it's not something a built-in will handle, since built-ins are for common cases. You have to roll your own. But since you're clearly a new programmer, that'll be good for you ;)

Seriously though, we won't tell you to write your own code unless we mean it, and we're usually happy to give students answers involving quick built-ins since those questions are in classes where they'll fail the assignment for using it anyway. Always always always show your code or we really have no choice but to assume you're lazy, since that's so common from beginners on forums. (It's easier to fix this kind of a reputation on a programming forum than anywhere else in the universe though, so don't panic.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Parsing a file

Postby macpy » Thu Jul 11, 2013 9:25 pm

Hi,

Thank you for your help. I am working on my own code as suggested by you guys and I have a question. I have a string, which is one line from the input file stripped off of all the white spaces. It looks like below

current string: position=1,1,rotation=90,0,0,mass=120
desired string: position=1,1, rotation=90,0,0, mass=120 ## There are spaces before r and m here vs the current string

Now, I am using a regex like below

Code: Select all
def readPart(inString):
    print inString
    inStringEdit = re.sub(',[A-z]',', -',inString)
    print inStringEdit


I know that this not right. I am replacing the the alphabet immediately following a ',' by '-'. I cant figure out how to replace it with ', ' followed by whatever alphabet there was.

Thank you for your help
macpy
 
Posts: 4
Joined: Mon Jul 01, 2013 5:42 pm

Re: Parsing a file

Postby ochichinyezaboombwa » Fri Jul 12, 2013 6:20 pm

Try to use
Code: Select all
inString.split(',')

-- maybe it'll be easier than re.
Also, for the future: tab ("t\") is a much better separator than space(" "), for many–many reasons.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests