Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.

2012-09-19 Thread Gregory Lund
more info:


 Consider it done, i have a new .py file saved as 'unzip_twice.py'
but it would be easier (the whole purpose of this task) is to have a
stand alone script that would work without having to open up the
shell.
In ArcGIS, I call the script by double clicking on my tool, selecting
the .zip in a GUI and it runs, extracting the first zipfile, but hangs
on the next, supposedly because of 'permissions' but 'permissions'
were not really an issue.
I think it's the is it a .zip or is it not a zip issue.
Permissions were not an issue in the original task, which worked (but
had flawed data structure (my fault)).


 Maybe its the version of Python?
 Maybe I didn't read what you wrote to type into the shell properly?
 Maybe it's the spaces and dashes in the zipfile name? (the LMS does
 that (learning Management system)
Maybe it's the location of the unzip_twice.py script?


 (the folder must already exist), it may do what you want. Make sure
 you test it on a sample set of data, not the real thing.
yes, folder existed

 Good luck and don't worry about asking dumb questions, the only dumb
 question is Was it you or your brother that was killed in the war?

 Thanks, as you can tell, I need it!
Thanks again, I really do need it! (oh sorry, was that obvious? haha!)

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip

2012-08-07 Thread Gregory Lund
# neophyte .py/pyTutor user.
# I am also a university GIS lecturer. My students submit their lab
assignments (zipped up into one zipfile) to a depository, which I
download as a single zip containing all 40 students' zipfiles.
# Goal: unzip a zipfile of student zipfiles and then unzip each
students' zipfile ( to prevent me from having to unzip each students'
zipfile by hand. )
# eventually I'll turn this into a tool in ArcGIS, that will grade
some of their assignment, but for now, just want to get these extract
processes running in python.

# using (and want to use) 2.6 because I'm working with ArcGIS
# this is what I've got thus far:

#1 Unzip a single starting zipfile initial_Zipfile (that contains a
series of 40 individual zipfiles) into a single folder named whatever
it was originally named.

#2 Unzip all of the zipfiles within the 'initial_Zipfile' into their own folder.

## Actual  Code:

import zipfile, arcpy, os.path, os

#1 Defining a function to Unzip the initial (.zip) folder. (this worked fine)

def unzip_Orig(oZ):
z = zipfile.ZipFile(oZ)
z.extractall()
return

q = unzip_Unzip_Data_Test.zip
unzip_Orig(q)

#2 my attempts to unzip each zipfile within the new folder below
(failed thus far)

mywd = os.getcwd()
zipList = os.listdir(mywd)
print zipList  #this didn't work, it printed the directory of where
the .py was stored.
#Zipfile.printdir() # returned 'Zipfile' is not defined.

#thoughts?

# Thanks
# Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How do I go from a textFile.txt to a list [] AND from a textFile.txt to a dictionary of dictionaries?

2012-07-22 Thread Gregory Lund
Only my second try to post to this list.
This is a dumb question with a simple (I presume) solution...
but...
how do I take a txt file with semicolon separated values and create a
dictionary and a list?
( in 2.6 because I'm working with ArcGIS.)

text file is as follows: (saved as text_data.txt) (it's longer,
this is just a sample)

ObjID;Xval;Yval;LineID;PolyID
0;1279027.0;246427.9375;0;1
1;1279069.625;246433.234375;0;1
2;1279091.0;246435.046875;1;1
3;1279112.5;246436.3125;1;1
4;1279134.0;246437.0;2;1
5;1279176.875;246436.71875;2;1
6;1279198.375;246435.734375;3;1
7;1279219.75;246434.1875;3;1

I'd like to create a list from said text data...
and a dictionary of dictionaries from said data.

Eventually I will create a point, line and polygon shapefiles from this file.
With the data above, it would be an 8 point pnt file, a 3 line line
file and 1 polygon. (point 7 connecting back to point 0)

Right now, I'm just trying to get started by creating a list and a
dictionary (to see which I want to use when creating my shapefiles.)

And, I need to do it in 2.6 because I'm working with ArcGIS.

Thanks in advance for your thoughts?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reading a csv of coordinates, trying to write a csv of bearings.

2012-07-08 Thread Gregory Lund
I'm Not looking for an absolute solution, but perhaps some insight
into some snippets of code, or
suggestions of where I should seek out answers to this issue.
Or where I've gone wrong below.
NOTE: currently this 'code' below reads my file and writes a file, but
what it's doing in the middle isn't what it's supposed to do (I know
that!) I just modified what I had to at least read/write, now I need
to add the atan2() stuff.

I'm currently reading 'learning python' (Lutz) for 2.6 (not 3.0!)
I must use 2.6 because that is what esri's ArcGIS 10.0 uses.
I'm Doing (1/2 way through)  the non-programmers tutorial for python
2.6, hoping to 'graduate' to the
http://docs.python.org/release/2.6.8/tutorial/index.html once I get
through the non-programmers tutorial.

Objectives: read a CSV file of coordinates, and generate a
new CSV file of bearings.

Provided: A valid CSV file with coordinates (every other line blank,
if that matters)

Intended result: A valid CSV file with bearings

Logic?: one function, accepting a filename for the input, and a filename
for the output (i.e. Coord_to_bearing(infile, outfile))

Caveats:
A bearing is declination from north (meaning the angle between the
direction and north).
2 points define a line (y = mx + b). (obviously)
Supposedly atan2() (from import math) will be in the code
http://en.wikipedia.org/wiki/Atan2

Sample input:
0,1,1

1,2,2

2,3,3

3,4,3

4,1268144.125,226540.2969

5,1268463.75,226260.1563


Sample output:
OID1,OID2,ANGLE
0,1,45.0
1,2,45.0
2,3,90.0
3,4,?
4,5,?


Since I am a beginner, I re-used some code from a previous task, and
kept lots of it
commented out to use to help with future coding.
Partial Code written in pursuit of this goal (with limited success)
it's messy, and incorrect, but it's a start, maybe hurting more than 
helping.

'''
The code herin was initially written to find a nmea sample file
('nmea_sample.txt) within the same folder as this .py file.
It has been modified to read the giveN CSV file of coordinates, and
generate a new CSV file of bearings.

Python will try to find the first parameter (ie, coordinates.csv) in
the same folder in which the .py file is saved.
The second parameter is the output that will be saved in the same
folder automatically.
Put another way... The new .csv file will be saved to the same folder
in which  .py file is saved.
I could use user input but trying to keep it simple.


1 Define a function that will be run (he name of the file, in the
working directory so that Python
knows what file to find, and where.
2. open the file
3. line' = f.readline() establishes 'line' and reads/returns one line from f's
file, up to the end of the line.
4. read the file line by line (while loop)
5. assigning 'data' as splitting the line by commas (',')
6. Extract information by line
7. Calculating the Bearings #this step is missing
8. Creating a new CSV file.

# I left lots of comments and 'old' code in so that I could use this
as a learning experience.


'''
import math #importing math so that atan2 is available
#1 Establishing the filenames

def coord2bearing(coordFile,csvFile):   # defining a function that
will be used later, a return is required at the end when function is
done.
# these two parameters will be
filled when calling this function in the python Shell.
import csv #2importing the csv module that has the necessary tools
I need to read/create a csv file.

csv_writer = csv.writer(open(csvFile, 'wb'), delimiter = ',') #
not sure if I need the ',' in here yet?.
  #
creating a csv file by using csv module's tool called 'writer' and
'open';
  #
'csv_writer' is just a variable name that could be anything
  #
defining 'csv_writer' as an object that has attributes and tools
  #
Important Note: Using 'wb' instead of 'w' caused the csv file to have
line breaks.
myfile = open (coordFile) # open the original sample data file by
using the open function (coordinates2.csv) (original coordinates.csv
had blank lines)
filename = 'coordinates.csv'

#2 The 'open()' command opens the extablished filename.
f = open(filename)

#3 applying 'line' and the f.readline statement

line = f.readline() #3

#4 read the file line by line in a 'while' loop.
OID = 0 # setting the OID varible to zero
while (line): #...while there is a line to go to...

#5 assigning 'data' as splitting the line by commas (',')

data = line.split(',') #not sure yet how the .csv is read, if
it 'sees' a comma or not...?