LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2015, 11:37 AM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
python make array from list / or convert parenthesis to brackets


I open a text file and read the lines into a list. The result is something like this.

Quote:
( ttime
( ttimeA player 1 ( 17 18 ) )
( ttimeB player 1 ( 17 18 ) )
)
It would be very useful if I could convert the parenthesis to square brackets and then python to interpret the whole thing as a hierarchical list.

Quote:
[ ttime
[ ttimeA player 1 [ 17 18 ] ]
[ ttimeB player 1 [ 17 18 ] ]
]
Then I could access things very easily with bracket notation.

Is this possible? Hope my question is clear.
 
Old 11-12-2015, 12:25 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Well, the parentheses->to->brackets part is easy...
Code:
line.replace('(', '[').replace(')', ']')
 
Old 11-12-2015, 12:58 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I'm no python guru, but isn't that just tuple terminology? Could you not just leave it as is and then loop or whatever over the tuple??
 
Old 11-12-2015, 01:14 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by grail View Post
I'm no python guru, but isn't that just tuple terminology? Could you not just leave it as is and then loop or whatever over the tuple??
I had not thought of that. However, the four lines are divided by pythons readline function into separate entries in a list. I do not know how to convert the lines into a 'proper' tuple.
 
Old 11-12-2015, 04:05 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
This really isn't as simple a problem as you think. You really should be using PyParsing to do it.
 
Old 11-13-2015, 04:28 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

Not really sure what your goal is here, but wouldn't a dicionary perhaps be better suited for this task?

Something like this:
Code:
>>> myDict
{'ttime': {'ttimeA': {'player1': ['17', '18']}}}
Then you can access the various values with something like:
Code:
>>> myDict['ttime']['ttimeA']['player1'][0]
'17'
And/or:
Code:
>>> myDict['ttime']['ttimeA']
{'player1': ['17', '18']}
Best regards,
HMW
 
Old 11-13-2015, 09:36 AM   #7
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by dugan View Post
This really isn't as simple a problem as you think. You really should be using PyParsing to do it.
You're right. Unfortunately, from the corporate network we don't have pyparsing installed.

However, I was able to work-around the issue and extract the required data and place the results into python defined list. The code is ugly, even by poor programming standards, but it works.

Thanks to all for the feedback.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
The difference between parenthesis and brackets sarexpert Linux - Software 1 10-01-2015 07:24 PM
[SOLVED] python convert output to a list simhumcon Linux - Newbie 5 05-06-2014 02:47 AM
[SOLVED] ps aux command to list only files in brackets lovesuicide Linux - Newbie 11 09-20-2013 02:32 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
How to convert short array to char array? bvkim Programming 4 06-08-2010 09:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration