reading a file in python and storing it in a list

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

reading a file in python and storing it in a list

Postby diehard_jr » Mon Oct 28, 2013 10:12 pm

i have a file in the following format
Code: Select all
2000, 379, -2000 #17
-2000, -2000, -2000 #18
-2000, 379, -2000 #19
j
-2000 379 -1817 (*origin point, 20*)
j
-1112 379 -1791 #21
-1112 379 -1817 #22
j


I need to read in this file and then story it in a list where the j's represent a split in the points and the start of a sub list and i remove all the values on a line after the # or the () so i just of 3 numbers in each list

I need the values to be in the following format:
[[[2000, 379, -2000] [-2000, 379, -2000], [-2000, 379, -1817]], [[-2000, 379, -2000]], [[-1112, 379, -1791], [-1112, 379, -1817]]]

how could i go about doing this.
Last edited by micseydel on Mon Oct 28, 2013 10:13 pm, edited 1 time in total.
Reason: Locked OP.
diehard_jr
 
Posts: 4
Joined: Mon Oct 28, 2013 10:00 pm

Re: reading a file in python and storing it in a list

Postby micseydel » Mon Oct 28, 2013 10:21 pm

What attempt have you made toward your goal?
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: 1262
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: reading a file in python and storing it in a list

Postby diehard_jr » Mon Oct 28, 2013 10:34 pm

so far i have the following

Code: Select all
#reading in the file and split it at the ( and the #

with open('filename', 'r') as f:
    list1 = [re.split('[(#]+', line.strip())[0].split() for line in f]
    points = [[list(map(int, var)) for var in g] for k, g in groupby(list1, key=lambda x: len(x)==1) if k !=1]

for point in points:
drawPoints = [[l0], l[1]] for l in point] #removes the 3 point from the element in the list points

pygame.draw.lines(screen, white, False, drawPoints, 1 #draws the points on my window named screen


This code should get me my desired however it does not work for the line

j
-2000 379 -1817 # 21
j

it seems to not like the single point to draw.

because i get the error:
ValueError: points argument must contain more than 1 points

So i need another point to draw too but i am not sure how i can do this with what i have as a file.
diehard_jr
 
Posts: 4
Joined: Mon Oct 28, 2013 10:00 pm

Re: reading a file in python and storing it in a list

Postby ochichinyezaboombwa » Tue Oct 29, 2013 2:29 am

Please explain more clearly how do you come from your example input to your example output.
Specifically, if I understand that j separates sections (sub-lists), then how on Earth the section
Code: Select all
2000, 379, -2000 #17
-2000, -2000, -2000 #18
-2000, 379, -2000 #19
could become
Code: Select all
[[2000, 379, -2000] [-2000, 379, -2000], [-2000, 379, -1817]]

and section
Code: Select all
-2000 379 -1817 (*origin point, 20*)
--
Code: Select all
[[-2000, 379, -2000]]
?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: reading a file in python and storing it in a list

Postby diehard_jr » Tue Oct 29, 2013 12:02 pm

ochichinyezaboombwa wrote:Please explain more clearly how do you come from your example input to your example output.
Specifically, if I understand that j separates sections (sub-lists), then how on Earth the section
Code: Select all
2000, 379, -2000 #17
-2000, -2000, -2000 #18
-2000, 379, -2000 #19
could become
Code: Select all
[[2000, 379, -2000] [-2000, 379, -2000], [-2000, 379, -1817]]

and section
Code: Select all
-2000 379 -1817 (*origin point, 20*)
--
Code: Select all
[[-2000, 379, -2000]]
?



@ochichinyezaboombwa
wow sorry for the misunderstanding it would help if I posted the output I desired correctly the output I want is as follows:


[[[2000, 379, -2000] [-2000, -2000, -2000], [-2000, 379, -2000]], [[-2000, 379, -1817]], [[-1112, 379, -1791], [-1112, 379, -1817]]]

so the j seperates each set of points into a sub list.
diehard_jr
 
Posts: 4
Joined: Mon Oct 28, 2013 10:00 pm

Re: reading a file in python and storing it in a list

Postby ochichinyezaboombwa » Tue Oct 29, 2013 7:13 pm

Code: Select all
import sys
lists = [[]]
for ln in open(sys.argv[1]):
    if ln.startswith('j'):
        lists.append([])
        continue
    ok = ln.split("#") [0] .split("(")[0]
    lists[-1].append( ok.split() )

print lists
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: reading a file in python and storing it in a list

Postby diehard_jr » Tue Oct 29, 2013 8:08 pm

ochichinyezaboombwa wrote:
Code: Select all
import sys
lists = [[]]
for ln in open(sys.argv[1]):
    if ln.startswith('j'):
        lists.append([])
        continue
    ok = ln.split("#") [0] .split("(")[0]
    lists[-1].append( ok.split() )

print lists


Ok thanks this fix my format issue and i get the output i desired:

Code: Select all
[[[2000, 379, -2000] [-2000, -2000, -2000], [-2000, 379, -2000]], [[-2000, 379, -1817]], [[-1112, 379, -1791], [-1112, 379, -1817]]]


my next question would be how can i then takes these points and then draw them using pygame.draw.line or pygame.draw.lines:
I have taken the points remove the third number from each set using the following code (points is the name of the list I created):

Code: Select all
drawPoints = [[l[0], l[1]] for l in points]


and I now have just the first 2 numbers in each element of the list together so i can use them as xy coordinates.
I am then drawing them to a screen using:

Code: Select all
pygame.draw.lines(screen, white, False, drawPoints, 5)


however when I compile this code I get an error:

Code: Select all
TypeError: points argument must contain more than 1 points


I believe this is because of the point [[-2000, 379]] because it is a single point with no connection to another point and draw.lines needs two points to draw a line.

So is their a way I can draw all of my points onto a screen without this error occuring.

And thanks again for your help
diehard_jr
 
Posts: 4
Joined: Mon Oct 28, 2013 10:00 pm


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests