[Tutor] Extract image from RTF file
I have a large amount of RTF files where the only thing in them is an image. I would like to extract them an save them as a png. Eventually, I would like to also grab some text that is on the image. I think PIL has something for this. Does anyone have any suggestion on how to start this? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] extracting lines in large file
I am trying to output all the lines that start with a specific word. It is a large output file (~14 Mb), but nothing that I thought would be a problem. for line in open('output.new'): i_line = line.split() if i_line: if i_line[0] == "intrinsic": print i_line It does not get all of the lines, it stops at line 130323. There are ~26 line. Is there a limit to the number of lines you can read in this way, or am I overlooking something else. Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] extracting lines in large file
tried both again, they both return the same 9 lines, when i expect 492. it dies on a blank line, but the if i_line takes care of the previous ones. On Mon, Jun 22, 2009 at 4:21 PM, vince spicer wrote: > 14mb file shouldn't be an issue, unless you very little ram, is there any > errors being outputted? > > a cleaner way for reading the file: > > for line in open("output.new"): > if line.startswith("intrinsic"): > print line > > > On Mon, Jun 22, 2009 at 2:16 PM, Bryan Fodness > wrote: >> >> I am trying to output all the lines that start with a specific word. >> It is a large output file (~14 Mb), but nothing that I thought would >> be a problem. >> >> for line in open('output.new'): >> i_line = line.split() >> if i_line: >> if i_line[0] == "intrinsic": >> print i_line >> >> It does not get all of the lines, it stops at line 130323. There are >> ~26 line. Is there a limit to the number of lines you can read in >> this way, or am I overlooking something else. >> >> Bryan >> ___ >> Tutor maillist - tu...@python.org >> http://mail.python.org/mailman/listinfo/tutor > > -- “The game of science can accurately be described as a never-ending insult to human intelligence.” - João Magueijo "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. " -Albert Einstein ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] accessing data in a usable format
I have a data file 'data1.dat', *a* *b**c* *d* 1 0.10.110.111 2 0.20.220.222 3 0.30.330.333 9 0.90.990.999 and I want to be able to access the values of *b*, *c*, or *d* depending on a value of *a*. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] accessing data in a usable format
> > Thank you both options work easily with my problem. Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] populating an array or using a dictionary
The data file is larger than shown, and I was wondering if it would be better to populate an array or create a dictionary. Which would be easier? On 10/19/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > Bryan Fodness wrote: > > I have a data file that I would like to extract data from: > > > > FS 1 2 3 4 5 > > 1.5 1.000 1.000 1.000 1.000 1.000 > > 2.0 0.985 0.994 0.997 0.996 0.996 > > 2.5 0.967 0.976 0.981 0.981 0.982 > > 3.0 0.949 0.958 0.965 0.966 0.967 > > 3.5 0.925 0.937 0.945 0.948 0.951 > > 4.0 0.901 0.916 0.925 0.930 0.934 > > 4.5 0.882 0.896 0.906 0.912 0.917 > > 5.0 0.863 0.876 0.887 0.893 0.899 > > > > > > The first row is a variable d, and the first column is FS. Any > > suggestions on the best way to do this. > > This looks the same as the question you asked on Wednesday...do you have > some code already? > > It helps if you can show what you have already done and ask for help > with changes. > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] populating an array or using a dictionary
I have a data file that I would like to extract data from: FS 1 2 3 4 5 1.5 1.000 1.000 1.000 1.000 1.000 2.0 0.985 0.994 0.997 0.996 0.996 2.5 0.967 0.976 0.981 0.981 0.982 3.0 0.949 0.958 0.965 0.966 0.967 3.5 0.925 0.937 0.945 0.948 0.951 4.0 0.901 0.916 0.925 0.930 0.934 4.5 0.882 0.896 0.906 0.912 0.917 5.0 0.863 0.876 0.887 0.893 0.899 The first row is a variable d, and the first column is FS. Any suggestions on the best way to do this. Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] populating an array or using a dictionary
Here is my code. dic2 = {} for line in file('21Ex6MV_tmr.dat'): d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, fs13, fs14, fs15, fs16, fs17, fs18, fs19, fs20, fs21, fs22, fs23, fs24, fs25, fs26, fs27, fs28, fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, fs37, fs38, fs39, fs40 = line.split() dic2[d] = (float(d), float(fs1), float(fs2), float(fs3), float(fs4), float(fs5), float(fs6), float(fs7), float(fs8), float(fs9), float(fs10), float(fs11), float(fs12), float(fs13), float(fs14), float(fs15), float(fs16), float(fs17), float(fs18), float(fs19), float(fs20), float(fs21), float(fs22), float(fs23), float(fs24), float(fs25), float(fs26), float(fs27), float(fs28), float(fs29), float(fs30), float(fs31), float(fs32), float(fs33), float(fs34), float(fs35), float(fs36), float(fs37), float(fs38), float(fs39), float(fs40)) print dic2[d] and, here is my error Check\eDoseCheck.py", line 44, in d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, NameError: name 'd' is not defined ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] populating an array or using a dictionary
it doesn't fix the problem, now it says there is a syntax error on the equal sign. On 10/21/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > > "Bryan Fodness" <[EMAIL PROTECTED]> wrote in > > >d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, > >fs11, fs12, fs13, fs14, fs15, fs16, fs17, fs18, fs19, > >fs20, fs21, fs22, fs23, fs24, fs25, fs26, fs27, fs28, > >fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, fs37, > >fs38, fs39, fs40 = line.split() > > Because your tuple spans multiple lines I think you > will need to put parens round it. > > >>> a,b,c,d > Traceback (most recent call last): > File "", line 1, in ? > NameError: name 'a' is not defined > >>> (a,b,c,d > ... > > So starting with a paren tells Python to ignore the newline. > Without it it gets confused about what you want to do. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] populating an array or using a dictionary
it works well if it is on the same line, but I would like to wrap it for readability On 10/21/07, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > it doesn't fix the problem, now it says there is a syntax error on the > equal sign. > > On 10/21/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > > > > > "Bryan Fodness" <[EMAIL PROTECTED]> wrote in > > > > >d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, > > >fs11, fs12, fs13, fs14, fs15, fs16, fs17, fs18, fs19, > > >fs20, fs21, fs22, fs23, fs24, fs25, fs26, fs27, fs28, > > >fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, fs37, > > >fs38, fs39, fs40 = line.split() > > > > Because your tuple spans multiple lines I think you > > will need to put parens round it. > > > > >>> a,b,c,d > > Traceback (most recent call last): > > File "", line 1, in ? > > NameError: name 'a' is not defined > > >>> (a,b,c,d > > ... > > > > So starting with a paren tells Python to ignore the newline. > > Without it it gets confused about what you want to do. > > > > HTH, > > > > -- > > Alan Gauld > > Author of the Learn to Program web site > > http://www.freenetpages.co.uk/hp/alan.gauld > > > > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] calling a variable name
I want to get a variable name dependent on another variable. I have tried, 'fs' + str(int(round(unblockedFS))) for fs13 and I get an invalid literal. If I code in the fs13, everything works. Is it possible to do this? unblockedFS=13.4 for line in file('21Ex6MV_tmr.dat'): d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, fs13, fs14, fs15, fs16, fs17, fs18 = line.split() if float(d) == round(calc_depth): b = float(fs13) print float(fs13) Thanks, Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling a variable name
Here is the actual snippet of code calc_depth =8.1 # which is actually d unblockedFS = 13.4 # which is the indexed fs for line in file('21Ex6MV_tmr.dat'): d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, fs13, fs14, fs15, fs16, fs17, fs18, fs19, fs20, fs21, fs22, fs23, fs24, fs25, fs26, fs27, fs28, fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, fs37, fs38, fs39, fs40 = line.split() if float(d) == round(calc_depth): print float('fs' + str(int(round(unblockedFS On 10/22/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > "Bryan Fodness" <[EMAIL PROTECTED]> wrote > > >I want to get a variable name dependent on another variable. > > Thats usually a bad idea, but... > > > I have tried, > > > > 'fs' + str(int(round(unblockedFS))) for fs13 > > I have no idea what you think this will do. > It gives a syntax error for me, which is what I expected. > > > and I get an invalid literal. > > Can you post real code and real error messages please? > > > If I code in the fs13, everything works. Is > > it possible to do this? > > I'm not sure because I'm not sure what you are really > trying to do. Creating new variable names on the fly > is usually not the best approach but without a context > we can't offer an alternative. The code snippet above > is no help since it is not valid Python. > > > unblockedFS=13.4 > > > > for line in file('21Ex6MV_tmr.dat'): > >d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, > > fs13, > > fs14, fs15, fs16, fs17, fs18 = line.split() > > BTW, Since all your variables are of the form fsNN it is probably > as easy(and less typing) to do: > > fs = line.split() > > and just refer to them by index (extracting d if required): > > >if float(d) == round(calc_depth): > > if float(fs[0]) == round(calc_depth) > >b = float(fs13) > > b = float(fs[13]) > > etc... > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling a variable name
Thank you. This works well. I am still trying to figure out the pros and cons of using an array, dictionary or list. On 10/22/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > Bryan Fodness wrote: > > Here is the actual snippet of code > > > > > > calc_depth =8.1 # which is actually d > > unblockedFS = 13.4 # which is the indexed fs > > > > for line in file('21Ex6MV_tmr.dat'): > > d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, > > fs13, fs14, fs15, fs16, fs17, fs18, fs19, fs20, fs21, fs22, fs23, fs24, > > fs25, fs26, fs27, fs28, fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, > > fs37, fs38, fs39, fs40 = line.split() > > if float(d) == round(calc_depth): > > print float('fs' + str(int(round(unblockedFS > > I think I would just keep the data in a list and index it: > > for line in file('21Ex6MV_tmr.dat'): > data = line.split() > d = data[0] > if float(d) == round(calc_depth): > print float(data[int(round(unblockedFS))]) > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] trouble with if
I have the following code, it keeps giving me a value of 1 for e. for line in file('21Ex6MV_oaf.dat'): oa, openoa, w15, w30, w45, w60 = line.split() if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > 15): e = float(openoa) else: e = 1 If I comment out the else, I get the correct value for line in file('21Ex6MV_oaf.dat'): oa, openoa, w15, w30, w45, w60 = line.split() if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > 15): e = float(openoa) #else: #e = 1 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] rounding to the nearest 0.5
Is there a built-in function that will round to the nearest 0.5? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with if
I cannot get this to work either. woffaxis = 7 if woffaxis != 0: woaf_pos = input("What is Wedge Direction (N/A, Lateral, Towards Heal, Towards Toe)?") if woaf_pos == 'Towards Toe': woffaxis = woffaxis elif woaf_pos == 'Towards Heal': woffaxis = (woffaxis * -1) else: woffaxis = 0 On 10/24/07, John Fouhy <[EMAIL PROTECTED]> wrote: > > On 25/10/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > I have the following code, it keeps giving me a value of 1 for e. > > > > for line in file('21Ex6MV_oaf.dat'): > > oa, openoa, w15, w30, w45, w60 = line.split() > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > > > 15): > > e = float(openoa) > > else: > > e = 1 > > > > If I comment out the else, I get the correct value > > > > for line in file('21Ex6MV_oaf.dat'): > > oa, openoa, w15, w30, w45, w60 = line.split() > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > > > 15): > > e = float(openoa) > > #else: > > #e = 1 > > Maybe you need a 'break' statement after 'e = float(openoa)'? > > As written, e will have whatever value is appropriate for the last > line of your input file. > > -- > John. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] manipulating data
I would like to have my data in a format so that I can create a contour plot. My data is in a file with a format, where there may be multiple fields field = 1 1a 0 2a 0 3a 5 4a 5 5a 5 6a 5 7a 5 8a 5 9a 0 10a 0 1b 0 2b 0 3b 5 4b 5 5b 5 6b 5 7b 5 8b 5 9b 0 10b 0 field = 2 1a 0 2a 0 3a 0 4a 4 5a 4 6a 4 7a 4 8a 0 9a 0 10a 0 1b 0 2b 0 3b 0 4b 4 5b 4 6b 4 7b 4 8b 0 9b 0 10b 0 field = 3 1a 0 2a 0 3a 0 4a 0 5a 3 6a 3 7a 0 8a 0 9a 0 10a 0 1b 0 2b 0 3b 0 4b 0 5b 3 6b 3 7b 0 8b 0 9b 0 10b 0 where, a b a b ab 10 00|00 00|00 00|00 9 00|00 00|00 00|00 8 01|10 00|00 00|00 7 01|10 00|00 00|00 6 01|10 00|00 000111|111000 5 01|10 00|00 000111|111000 4 01|10 00|00 00|00 3 01|10 00|00 00|00 2 00|00 00|00 00|00 1 00|00 00|00 00|00 I could possibly have many of these that I will add together and normalize to one. Also, there are 60 a and b blocks, the middle 40 are 0.5 times the width of the outer 20. I thought about filling an array, but there is not a one to one symmetry. I cannot seem to get my head around this. Can anybody help me get started? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manipulating data
I also have some information at the beginning of the file and between each field. Is there a way to get the info at the beginning and tell it once it sees Leaf 1A to read the values for the next 120 and then repeat until there are no more Fields. File Rev = G Treatment = Dynamic Dose Last Name = Fodness First Name = Bryan Patient ID = 0001 Number of Fields = 4 Number of Leaves = 120 Tolerance = 0.50 Field = 10 Index = 0. Carriage Group = 1 Operator = Collimator = 0.0 Leaf 1A = 0.00 Leaf 2A = 0.00 Leaf 3A = 0.00 Leaf 4A = 0.00 ... Leaf 57B = 0.00 Leaf 58B = 0.00 Leaf 59B = 0.00 Leaf 60B = 0.00 Note = 0 Shape = 4 500 500 500 -500 -500 -500 -500 500 Magnification = 1.00 Field = 8 Index = 0.4000 Carriage Group = 1 Operator = Collimator = 0.0 Leaf 1A = 0.00 Leaf 2A = 0.00 Leaf 3A = 0.00 Leaf 4A = 0.00 ... Leaf 57B = 0.00 Leaf 58B = 0.00 Leaf 59B = 0.00 Leaf 60B = 0.00 Note = 0 Shape = 4 400 400 400 -400 -400 -400 -400 400 Magnification = 1.00 I would like to have a data structure that I can use in one of the graphing utilities (matpolotlib?). I probably want to populate an array with values, but I have not figured out how I want to do that yet. On Nov 7, 2007 8:52 AM, Kent Johnson <[EMAIL PROTECTED]> wrote: > Bryan Fodness wrote: > > I would like to have my data in a format so that I can create a contour > > plot. > > > > My data is in a file with a format, where there may be multiple fields > > > > field = 1 > > > > 1a0 > > If your data is really this regular, it is pretty easy to parse. A > useful technique is to access a file's next method directly. Something > like this (not tested!): > > f = open('data.txt') > fields = {} # build a dict of fields > try: > while True: > # Get the field line > line = f.next() > field = int(line.split()[-1]) # last part of the line as an int > > f.next() # skip blank line > > data = {} # for each field, map (row, col) to value > for i in range(20): # read 20 data lines > line = f.next() > ix, value = f.split() > row = int(ix[:-1]) > col = ix[-1] > data[row, col] = int(value) > > fields[field] = data > > f.next() > except StopIteration: > pass > > This builds a dict whose keys are field numbers and values are > themselves dicts mapping (row, col) pairs to a value. > > > where, > > > >a b > > a b ab > > 1000|00 00|00 00|00 > > 9 00|00 00|00 00|00 > > 8 01|10 00|00 00|00 > > 7 01|10 00|00 00|00 > > 6 01|10 00|00 000111|111000 > > 5 01|10 00|00 000111|111000 > > 4 01|10 00|00 00|00 > > 3 01|10 00|00 00|00 > > 2 00|00 00|00 00|00 > > 1 00|00 00|00 00|00 > > I guess this is the intended output? Do you want to actually create a > printed table like this, or some kind of data structure that represents > the table, or what? > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manipulating data
Using, fields = {} for line in open('data.txt') : if line : if line.split()[0] == 'field' : field = int(line.split()[-1]) else : fields[field] = tuple(line.split()) I get, fields[field] = tuple(line.split()) NameError: name 'field' is not defined On Nov 8, 2007 7:34 AM, Ricardo Aráoz <[EMAIL PROTECTED]> wrote: > > Kent Johnson wrote: > > Bryan Fodness wrote: > >> I would like to have my data in a format so that I can create a contour > >> plot. > >> > >> My data is in a file with a format, where there may be multiple fields > >> > >> field = 1 > >> > >> 1a 0 > > > > If your data is really this regular, it is pretty easy to parse. A > > useful technique is to access a file's next method directly. Something > > like this (not tested!): > > > > f = open('data.txt') > > fields = {} # build a dict of fields > > try: > >while True: > > # Get the field line > > line = f.next() > > field = int(line.split()[-1]) # last part of the line as an int > > > > f.next() # skip blank line > > > > data = {} # for each field, map (row, col) to value > > for i in range(20): # read 20 data lines > >line = f.next() > >ix, value = f.split() > >row = int(ix[:-1]) > >col = ix[-1] > >data[row, col] = int(value) > > > > fields[field] = data > > > > f.next() > > except StopIteration: > >pass > > > > Or maybe just (untested) : > > fields = {} # build a dict of fields > for line in open('data.txt') : >if line :# skip blank lines >if line.split()[0] == 'field' : >field = int(line.split()[-1]) >else : >fields[field] = tuple(line.split()) > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manipulating data
Using the algorithm below, I get: Traceback (most recent call last): File "C:\Users\bryan\Documents\Yennes Medical Physics\mlcShape\findvalue.py", line 49, in file.next() TypeError: descriptor 'next' of 'file' object needs an argument And, it is true that I am trying to build a list and not overwrite the value. On Nov 12, 2007 5:22 PM, ALAN GAULD <[EMAIL PROTECTED]> wrote: > Brian, > > > if line.split()[0] == 'Field': > >field = int(line.split()[-1]) > > > > IndexError: list index out of range > > > You have blank lines in the file, when you try to call split > on an empty string you get an empty list so trying to > index any element will result in an Index error. > > That's why I suggested using exceptions, testing for > every possible error condition could take a long time > and be error prone. Unfortunately I guessed the wrong > error code and didn't realise you had some dross to > wade through first... so its a wee bit more complex. > > Personally I'd use a flag to detect when field had > been found and set - ie set field to None and then > test for that changing, then test for Leaf as you do. > > So I think your algorithm should be > > for line in file >if 'Field' in line: > field = int(line.split()[-1]) >elif 'Leaf' in line: > fields[field] = line.split()[-1] >else: file.next() > > But I think there's another problem in that you are > then overwriting the value of Leaf when I think you > are trying to build a list? I'm not 100% sure what > you are aiming for but hopefully its some help! > > Alan G. > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manipulating data
I have tried, f = open('TEST1.MLC') fields = {} for line in f: the_line = line.split() if the_line: if the_line[0] == 'Field': field = int(the_line[-1]) elif the_line[0] == 'Leaf': fields[field] = the_line[-1] which, sort of works, but it overwrites each value. On Nov 12, 2007 6:55 PM, Alan Gauld <[EMAIL PROTECTED]> wrote: > The lesson here is not to try to do two things at once... > > > file.next() > > TypeError: descriptor 'next' of 'file' object needs an argument > > OK, My algorithm was meant to be pseudo code so file was > not intended to be taken literally, its just a marker for an open > file object. > > > And, it is true that I am trying to build a list and not overwrite > > the value. > > OK, That adds a bit more tweaking... > > >> Personally I'd use a flag to detect when field had > >> been found and set - ie set field to None and then > >> test for that changing, then test for Leaf as you do. > > That was before I went back to testing my own project > > >> So I think your algorithm should be > >> > >> for line in file > >>if 'Field' in line: > >> field = int(line.split()[-1]) > > and this was after - with no flag anywhere in sight! Oops. > > I intended the if test to include a check for field == None... > > if field == None and 'Field' in line: > > >>elif 'Leaf' in line: > >> fields[field] = line.split()[-1] > >>else: file.next() > >> > >> But I think there's another problem in that you are > >> then overwriting the value of Leaf when I think you > >> are trying to build a list? > > So we need to create an empty list entry where we > define field and then append here, so my pseudo > code now becomes: > > f = open('foo.dat') > for line in f: >if field == None and 'Field' in line: > field = int(line.split()[-1]) > fields[field] = [] >elif 'Leaf' in line: > fields[field].append(line.split()[-1]) >else: f.next() > > still untested I'm afraid, so it still may not work. > > HTH, > > Alan G. > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] NumPy Question - numpy.put in multi-dimensional array
I see how to do it in a one-dimenstional array, but do not know the syntax for the multi-dimensional case. from numpy import * a = zeros((60,40), int) fields = {} field = 10 fields[field] = '30A', 5 iy = int(fields[field][1]) ix = int(fields[field][0].rstrip('AB')) for j in range(iy): put(a,[39 - j],[1]) Can someone help me figure out how I would do it for multiple rows? I thought, for i in range(ix): for j in range(iy): put(a,[i][39-j],[1]) but, Traceback (most recent call last): put(a,[i][39 - j],[1]) IndexError: list index out of range ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] NumPy Question - numpy.put in multi-dimensional array
Thank you. That works great! On Nov 13, 2007 7:18 PM, Eike Welk <[EMAIL PROTECTED]> wrote: > Hello Bryan! > > On Wednesday 14 November 2007 00:18, Bryan Fodness wrote: > > I see how to do it in a one-dimenstional array, but do not know the > > syntax for the multi-dimensional case. > > > > >from numpy import * > > > > a = zeros((60,40), int) > > > > fields = {} > > field = 10 > > fields[field] = '30A', 5 > > > > iy = int(fields[field][1]) > > ix = int(fields[field][0].rstrip('AB')) > > > > for j in range(iy): > > put(a,[39 - j],[1]) > Should be maybe: > a[0, 39 - j] = 1 > > > > > Can someone help me figure out how I would do it for multiple rows? > > > > I thought, > > > > for i in range(ix): > >for j in range(iy): > > put(a,[i][39-j],[1]) > change to: > a[i, 39-j] = 1 > > You could replace the nested for loops by the following code: > a[:ix, :iy:-1] = 1 > > I think you shouldn't use put(...) in your code. It is a fairly > specialized function. > > More information: > http://www.scipy.org/Numpy_Example_List_With_Doc#head-5202db3259f69441c695ab0efc0cdf45341829fc > > Regards, > Eike > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manipulating data
I try this, f = open('TEST1.MLC') fields = {} for line in f: if line.split()[0] == 'Field': field = int(line.split()[-1]) elif line.split()[0] == 'Leaf': fields[field] = line.split()[-1] else: line = f.next() and get, Traceback (most recent call last): File "", line 1, in line.split()[0] IndexError: list index out of range I have attached my data file. File Rev = G Treatment = Dynamic Dose Last Name = Fodness First Name = Bryan Patient ID = 0001 Number of Fields = 4 Number of Leaves = 120 Tolerance = 0.50 Field = 10 Index = 0. Carriage Group = 1 Operator = Collimator = 0.0 Leaf 1A = 0.00 Leaf 2A = 0.00 Leaf 3A = 0.00 Leaf 4A = 0.00 Leaf 5A = 0.00 Leaf 6A = 0.00 Leaf 7A = 0.00 Leaf 8A = 0.00 Leaf 9A = 0.00 Leaf 10A = 0.00 Leaf 11A = 0.00 Leaf 12A = 0.00 Leaf 13A = 0.00 Leaf 14A = 0.00 Leaf 15A = 0.00 Leaf 16A = 0.00 Leaf 17A = 0.00 Leaf 18A = 0.00 Leaf 19A = 0.00 Leaf 20A = 0.00 Leaf 21A = 5.00 Leaf 22A = 5.00 Leaf 23A = 5.00 Leaf 24A = 5.00 Leaf 25A = 5.00 Leaf 26A = 5.00 Leaf 27A = 5.00 Leaf 28A = 5.00 Leaf 29A = 5.00 Leaf 30A = 5.00 Leaf 31A = 5.00 Leaf 32A = 5.00 Leaf 33A = 5.00 Leaf 34A = 5.00 Leaf 35A = 5.00 Leaf 36A = 5.00 Leaf 37A = 5.00 Leaf 38A = 5.00 Leaf 39A = 5.00 Leaf 40A = 5.00 Leaf 41A = 0.00 Leaf 42A = 0.00 Leaf 43A = 0.00 Leaf 44A = 0.00 Leaf 45A = 0.00 Leaf 46A = 0.00 Leaf 47A = 0.00 Leaf 48A = 0.00 Leaf 49A = 0.00 Leaf 50A = 0.00 Leaf 51A = 0.00 Leaf 52A = 0.00 Leaf 53A = 0.00 Leaf 54A = 0.00 Leaf 55A = 0.00 Leaf 56A = 0.00 Leaf 57A = 0.00 Leaf 58A = 0.00 Leaf 59A = 0.00 Leaf 60A = 0.00 Leaf 1B = 0.00 Leaf 2B = 0.00 Leaf 3B = 0.00 Leaf 4B = 0.00 Leaf 5B = 0.00 Leaf 6B = 0.00 Leaf 7B = 0.00 Leaf 8B = 0.00 Leaf 9B = 0.00 Leaf 10B = 0.00 Leaf 11B = 0.00 Leaf 12B = 0.00 Leaf 13B = 0.00 Leaf 14B = 0.00 Leaf 15B = 0.00 Leaf 16B = 0.00 Leaf 17B = 0.00 Leaf 18B = 0.00 Leaf 19B = 0.00 Leaf 20B = 0.00 Leaf 21B = 5.00 Leaf 22B = 5.00 Leaf 23B = 5.00 Leaf 24B = 5.00 Leaf 25B = 5.00 Leaf 26B = 5.00 Leaf 27B = 5.00 Leaf 28B = 5.00 Leaf 29B = 5.00 Leaf 30B = 5.00 Leaf 31B = 5.00 Leaf 32B = 5.00 Leaf 33B = 5.00 Leaf 34B = 5.00 Leaf 35B = 5.00 Leaf 36B = 5.00 Leaf 37B = 5.00 Leaf 38B = 5.00 Leaf 39B = 5.00 Leaf 40B = 5.00 Leaf 41B = 0.00 Leaf 42B = 0.00 Leaf 43B = 0.00 Leaf 44B = 0.00 Leaf 45B = 0.00 Leaf 46B = 0.00 Leaf 47B = 0.00 Leaf 48B = 0.00 Leaf 49B = 0.00 Leaf 50B = 0.00 Leaf 51B = 0.00 Leaf 52B = 0.00 Leaf 53B = 0.00 Leaf 54B = 0.00 Leaf 55B = 0.00 Leaf 56B = 0.00 Leaf 57B = 0.00 Leaf 58B = 0.00 Leaf 59B = 0.00 Leaf 60B = 0.00 Note = 0 Shape = 4 500 500 500 -500 -500 -500 -500 500 Magnification = 1.00 Field = 8 Index = 0.4000 Carriage Group = 1 Operator = Collimator = 0.0 Leaf 1A = 0.00 Leaf 2A = 0.00 Leaf 3A = 0.00 Leaf 4A = 0.00 Leaf 5A = 0.00 Leaf 6A = 0.00 Leaf 7A = 0.00 Leaf 8A = 0.00 Leaf 9A = 0.00 Leaf 10A = 0.00 Leaf 11A = 0.00 Leaf 12A = 0.00 Leaf 13A = 0.00 Leaf 14A = 0.00 Leaf 15A = 0.00 Leaf 16A = 0.00 Leaf 17A = 0.00 Leaf 18A = 0.00 Leaf 19A = 0.00 Leaf 20A = 0.00 Leaf 21A = 0.00 Leaf 22A = 0.00 Leaf 23A = 4.00 Leaf 24A = 4.00 Leaf 25A = 4.00 Leaf 26A = 4.00 Leaf 27A = 4.00 Leaf 28A = 4.00 Leaf 29A = 4.00 Leaf 30A = 4.00 Leaf 31A = 4.00 Leaf 32A = 4.00 Leaf 33A = 4.00 Leaf 34A = 4.00 Leaf 35A = 4.00 Leaf 36A = 4.00 Leaf 37A = 4.00 Leaf 38A = 4.00 Leaf 39A = 0.00 Leaf 40A = 0.00 Leaf 41A = 0.00 Leaf 42A = 0.00 Leaf 43A = 0.00 Leaf 44A = 0.00 Leaf 45A = 0.00 Leaf 46A = 0.00 Leaf 47A = 0.00 Leaf 48A = 0.00 Leaf 49A = 0.00 Leaf 50A = 0.00 Leaf 51A = 0.00 Leaf 52A = 0.00 Leaf 53A = 0.00 Leaf 54A = 0.00 Leaf 55A = 0.00 Leaf 56A = 0.00 Leaf 57A = 0.00 Leaf 58A = 0.00 Leaf 59A = 0.00 Leaf 60A = 0.00 Leaf 1B = 0.00 Leaf 2B = 0.00 Leaf 3B = 0.00 Leaf 4B = 0.00 Leaf 5B = 0.00 Leaf 6B = 0.00 Leaf 7B = 0.00 Leaf 8B = 0.00 Leaf 9B = 0.00 Leaf 10B = 0.00 Leaf 11B = 0.00 Leaf 12B = 0.00 Leaf 13B = 0.00 Leaf 14B = 0.00 Leaf 15B = 0.00 Leaf 16B = 0.00 Leaf 17B = 0.00 Leaf 18B = 0.00 Leaf 19B = 0.00 Leaf 20B = 0.00 Leaf 21B = 0.00 Leaf 22B = 0.00 Leaf 23B = 4.00 Leaf 24B = 4.00 Leaf 25B = 4.00 Leaf 26B = 4.00 Leaf 27B = 4.00 Leaf 28B = 4.00 Leaf 29B = 4.00 Leaf 30B = 4.00 Leaf 31B = 4.00 Leaf 32B = 4.00 Leaf 33B = 4.00 Leaf 34B = 4.00 Leaf 35B = 4.00 Leaf 36B = 4.00 Leaf 37B = 4.00 Leaf 38B = 4.00 Leaf 39B = 0.00 Leaf 40B = 0.00 Leaf 41B = 0.00 Leaf 42B = 0.00 Leaf 43B = 0.00 Leaf 44B = 0.00 Leaf 45B = 0.00 Leaf 46B = 0.00 Leaf 47B = 0.00 Leaf 48B = 0.00 Leaf 49B = 0.00 Leaf 50B = 0.00 Leaf 51B = 0.00 Leaf 52B = 0.00 Leaf 53B = 0.00 Leaf 54B
Re: [Tutor] how to accept an integer?
On Dec 5, 2007 4:16 PM, Jerry Hill <[EMAIL PROTECTED]> wrote: > On Dec 5, 2007 4:01 PM, Mahesh N <[EMAIL PROTECTED]> wrote: > > I dun understand the mistake. My aim is to accept an integer number. The > > python lookup in IDLE asks for a string object but the interpreter > returns > > with the following error message. Some one pls explain. > > Thank You > > > > PS : I understand that i can do type conversion after getting input > thru > > raw_input(). But how does input() function work? > > > > >>> prompt="temme a number\n" > > >>> speed =input(prompt) > > > > Traceback (most recent call last): > > File "", line 1, in > > speed =input(prompt) > > TypeError: 'str' object is not callable > > You have code that you haven't shown us. My crystal ball tells me > that somewhere above this point you did input = "Some String", thus > shadowing the builtin input function. Start a new interpreter and try > again and you should find that it works as expected. > > As I'm sure you'll hear from others, it really is best to use > raw_input instead. If you want an integer instead of a string, do > something like this: > > speed = int(raw_input(prompt)) Is this how ALL known integers should be input? > > > That way whatever the user types isn't run as python code, just read > in as a string and then converted into an integer. > > -- > Jerry > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] updating a print statement
I have a print statement in a for loop so I can watch the progress for line in file(file): the_line = line.split() if the_line: print ("Index = %.2f") %index Is there a way that only one line will be output and the variable is updated rather than one line for every index. Thanks, Bryan -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a print statement
Here is the code. for line in file('test.txt'): the_line = line.split() if the_line: if the_line[0] == 'Index': index = float(the_line[-1]) print ("\rIndex = %.3f") %index raw_input("\nExit") Here is the output. Index = 0.000 Index = 0.400 Index = 0.800 Index = 1.000 Exit On Dec 10, 2007 4:33 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > Bryan Fodness wrote: > > I do want to overwrite the same line. > > > > I do not see a difference between using the \r and not using it. > > How are you running the program? Try it from a command line if that is > not what you are doing. Can you show your new code? > > Kent > > PS Please Reply All to stay on the list. > > > > > On Dec 10, 2007 2:43 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>> wrote: > > > > Bryan Fodness wrote: > > > I have a print statement in a for loop so I can watch the > progress > > > > > > for line in file(file): > > > the_line = line.split() > > > if the_line: > > > print ("Index = %.2f") %index > > > > > > Is there a way that only one line will be output and the variable > is > > > updated rather than one line for every index. > > > > I'm not sure I understand what you are asking. index is never > > updated in > > the loop above. > > > > If you want to overwrite the same line on the console, try > > print "\rIndex = %.2f" % index, > > > > note ^ > > ---^ > > > > You might need some extra spaces at the end of the print to 'erase' > a > > longer previous line. > > > > Kent > > > > > > > > > > -- > > "The game of science can accurately be described as a never-ending > > insult to human intelligence." - João Magueijo > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a print statement
for line in file('test.txt'): the_line = line.split() if the_line: if the_line[0] == 'Index': index = float(the_line[-1]) print ("\rIndex = %.3f ") %index, raw_input("\nExit") Here is my output, Index = 0.000 Index = 0.400 Index = 0.800 Index = 1.000 On Dec 10, 2007 4:48 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > Bryan Fodness wrote: > > Here is the code. > > > > for line in file('test.txt'): > > the_line = line.split() > > if the_line: > > if the_line[0] == 'Index': > > index = float(the_line[-1]) > > print ("\rIndex = %.3f") %index > > Add a comma at the end of the above line to suppress the newline that > print normally outputs. > > Kent > > > raw_input("\nExit") > > > > Here is the output. > > > > Index = 0.000 > > Index = 0.400 > > Index = 0.800 > > Index = 1.000 > > > > Exit > > > > > > On Dec 10, 2007 4:33 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>> wrote: > > > > Bryan Fodness wrote: > > > I do want to overwrite the same line. > > > > > > I do not see a difference between using the \r and not using it. > > > > How are you running the program? Try it from a command line if that > is > > not what you are doing. Can you show your new code? > > > > Kent > > > > PS Please Reply All to stay on the list. > > > > > > > > On Dec 10, 2007 2:43 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]> > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>> wrote: > > > > > > Bryan Fodness wrote: > > > > I have a print statement in a for loop so I can watch the > > progress > > > > > > > > for line in file(file): > > > > the_line = line.split() > > > > if the_line: > > > > print ("Index = %.2f") %index > > > > > > > > Is there a way that only one line will be output and the > > variable is > > > > updated rather than one line for every index. > > > > > > I'm not sure I understand what you are asking. index is never > > > updated in > > > the loop above. > > > > > > If you want to overwrite the same line on the console, try > > > print "\rIndex = %.2f" % index, > > > > > > note ^ > > > ---^ > > > > > > You might need some extra spaces at the end of the print to > > 'erase' a > > > longer previous line. > > > > > > Kent > > > > > > > > > > > > > > > -- > > > "The game of science can accurately be described as a > never-ending > > > insult to human intelligence." - João Magueijo > > > > > > > > > > -- > > "The game of science can accurately be described as a never-ending > > insult to human intelligence." - João Magueijo > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a print statement
I ran it both in IDLE and Command Prompt On Dec 10, 2007 5:02 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > How are you running the program? > > Bryan Fodness wrote: > > > > for line in file('test.txt'): > > the_line = line.split() > > if the_line: > > if the_line[0] == 'Index': > > index = float(the_line[-1]) > > print ("\rIndex = %.3f ") %index, > > raw_input("\nExit") > > Here is my output, > > > > Index = 0.000 Index = 0.400 Index = 0.800 Index = 1.000 > > > > > > On Dec 10, 2007 4:48 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>> wrote: > > > > Bryan Fodness wrote: > > > Here is the code. > > > > > > for line in file('test.txt'): > > > the_line = line.split() > > > if the_line: > > > if the_line[0] == 'Index': > > > index = float(the_line[-1]) > > > print ("\rIndex = %.3f") %index > > > > Add a comma at the end of the above line to suppress the newline > that > > print normally outputs. > > > > Kent > > > > > raw_input("\nExit") > > > > > > Here is the output. > > > > > > Index = 0.000 > > > Index = 0.400 > > > Index = 0.800 > > > Index = 1.000 > > > > > > Exit > > > > > > > > > On Dec 10, 2007 4:33 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]> > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>> wrote: > > > > > > Bryan Fodness wrote: > > > > I do want to overwrite the same line. > > > > > > > > I do not see a difference between using the \r and not > > using it. > > > > > > How are you running the program? Try it from a command line > > if that is > > > not what you are doing. Can you show your new code? > > > > > > Kent > > > > > > PS Please Reply All to stay on the list. > > > > > > > > > > > On Dec 10, 2007 2:43 PM, Kent Johnson <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]> > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> > > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>>> wrote: > > > > > > > > Bryan Fodness wrote: > > > > > I have a print statement in a for loop so I can > > watch the > > > progress > > > > > > > > > > for line in file(file): > > > > > the_line = line.split() > > > > > if the_line: > > > > > print ("Index = %.2f") %index > > > > > > > > > > Is there a way that only one line will be output > > and the > > > variable is > > > > > updated rather than one line for every index. > > > > > > > > I'm not sure I understand what you are asking. index > > is never > > > > updated in > > > > the loop above. > > > > > > > > If you want to overwrite the same line on the console, > try > > > > print "\rIndex = %.2f" % index, > > > > > > > > note ^ > > > > ---^ > > > > > > > > You might need some extra spaces at the end of the > > print to > > > 'erase' a > > > > longer previous line. > > > > > > > > Kent > > > > > > > > > > > > > > > > > > > > -- > > > > "The game of science can accurately be described as a > > never-ending > > > > insult to human intelligence." - João Magueijo > > > > > > > > > > > > > > > -- > > > "The game of science can accurately be described as a > never-ending > > > insult to human intelligence." - João Magueijo > > > > > > > > > > -- > > "The game of science can accurately be described as a never-ending > > insult to human intelligence." - João Magueijo > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a print statement
It works now. Closed everything down and reopened. Thanks. On Dec 10, 2007 5:07 PM, Bryan Fodness <[EMAIL PROTECTED]> wrote: > I ran it both in IDLE and Command Prompt > > > On Dec 10, 2007 5:02 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > > > How are you running the program? > > > > Bryan Fodness wrote: > > > > > > for line in file('test.txt'): > > > the_line = line.split() > > > if the_line: > > > if the_line[0] == 'Index': > > > index = float(the_line[-1]) > > > print ("\rIndex = %.3f ") %index, > > > raw_input("\nExit") > > > Here is my output, > > > > > > Index = 0.000 Index = 0.400 Index = 0.800 Index = 1.000 > > > > > > > > > On Dec 10, 2007 4:48 PM, Kent Johnson <[EMAIL PROTECTED] > > > <mailto:[EMAIL PROTECTED]>> wrote: > > > > > > Bryan Fodness wrote: > > > > Here is the code. > > > > > > > > for line in file(' test.txt'): > > > > the_line = line.split() > > > > if the_line: > > > > if the_line[0] == 'Index': > > > > index = float(the_line[-1]) > > > > print ("\rIndex = %.3f") %index > > > > > > Add a comma at the end of the above line to suppress the newline > > that > > > print normally outputs. > > > > > > Kent > > > > > > > raw_input("\nExit") > > > > > > > > Here is the output. > > > > > > > > Index = 0.000 > > > > Index = 0.400 > > > > Index = 0.800 > > > > Index = 1.000 > > > > > > > > Exit > > > > > > > > > > > > On Dec 10, 2007 4:33 PM, Kent Johnson <[EMAIL PROTECTED] > > > <mailto:[EMAIL PROTECTED] > > > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>> wrote: > > > > > > > > Bryan Fodness wrote: > > > > > I do want to overwrite the same line. > > > > > > > > > > I do not see a difference between using the \r and not > > > using it. > > > > > > > > How are you running the program? Try it from a command line > > > if that is > > > > not what you are doing. Can you show your new code? > > > > > > > > Kent > > > > > > > > PS Please Reply All to stay on the list. > > > > > > > > > > > > > > On Dec 10, 2007 2:43 PM, Kent Johnson < [EMAIL PROTECTED] > > > <mailto:[EMAIL PROTECTED]> > > > > <mailto:[EMAIL PROTECTED] > > > > > > <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > > > mailto:[EMAIL PROTECTED]>>>> wrote: > > > > > > > > > > Bryan Fodness wrote: > > > > > > I have a print statement in a for loop so I can > > > watch the > > > > progress > > > > > > > > > > > > for line in file(file): > > > > > > the_line = line.split() > > > > > > if the_line: > > > > > > print ("Index = %.2f") %index > > > > > > > > > > > > Is there a way that only one line will be output > > > and the > > > > variable is > > > > > > updated rather than one line for every index. > > > > > > > > > > I'm not sure I understand what you are asking. index > > > > > is never > > > > > updated in > > > > > the loop above. > > > > > > > > > > If you want to overwrite the same line on the > > console, try > > > > > print "\rIndex = %.2f" % index, > > > > > > > > > > note ^ > > > > > ---^ > > > > > > > > > > You might need some extra spaces at the end of the > > > print to > > > > 'erase' a > > > > > longer previous line. > > > > > > > > > > Kent > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > "The game of science can accurately be described as a > > > never-ending > > > > > insult to human intelligence." - João Magueijo > > > > > > > > > > > > > > > > > > > > -- > > > > "The game of science can accurately be described as a > > never-ending > > > > insult to human intelligence." - João Magueijo > > > > > > > > > > > > > > > -- > > > "The game of science can accurately be described as a never-ending > > > insult to human intelligence." - João Magueijo > > > > > > > -- > > "The game of science can accurately be described as a never-ending insult > to human intelligence." - João Magueijo > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Parsing DICOMRT file
I am trying to parse a DICOMRT file, which is a radiation therapy DICOM file. First, I get different outputs from the two methods below. for line in file('file.dcm', 'rb'): print line inp = open('file.dcm', 'rb') print inp.readlines() Second, I have never tried to parse a binary file. I could use a few hints to get started. Bryan -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing DICOMRT file
Thanks for the immediate response! On Dec 12, 2007 5:57 PM, John Fouhy <[EMAIL PROTECTED]> wrote: > On 13/12/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > I am new to doing anything like this. I have looked at > > http://www.leadtools.com/SDK/Medical/DICOM/ltdc1.htm and am > > not sure how to proceed. > > I haven't much experience here, but this is how I'd proceed, I think: > > 1. Start by reading the file. It's binary data (I guess) so there's > no point in reading lines.: > rawData = open('file.dcm', 'rb').read() > > 2. Write a function to parse the preamble: > > def parsePreamble(data): >preamble = data[:128] >dicm = data[128:132] > ># you might need to read up on encodings and things to make sure > this test is valid >if dicm == 'DICM': > return preamble, 132 >else: > raise NotAPreambleException > > 3. Write functions to parse data elements. The functions are going to > try to parse a data element starting at a particular position, and if > successful, return the position of the end of the element. > > def parseDataelement(data, start): ># do stuff -- the web page you linked didn't have enough information > here >return element, pos > > 4. Parse the whole thing; > > def parseDICOM(data): >elements = [] >try: > preamble, next = parsePreamble(data) >except NotAPreambleException: > preamble, next = None, 0 > >while True: > element, next = parseDataElement(data, next) > elements.append(element) > # you will need some way of breaking out of this loop, either by > checking the structure of > # element for an end condition, or by parseDataElement raising > an exception. > >return elements # and maybe preamble too if you want it > > HTH! > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing DICOMRT file
Just downloaded it and have not had a chance to check it out. Thanks, Bryan On Dec 12, 2007 6:18 PM, Eric Brunson <[EMAIL PROTECTED]> wrote: > Bryan Fodness wrote: > > I am trying to parse a DICOMRT file, which is a radiation therapy > > DICOM file. > > I'm a little late to the party, but you may want to take a look at this: > > http://mypage.iu.edu/~mmiller3/python/#dycom > > > > > > > First, I get different outputs from the two methods below. > > > > for line in file('file.dcm', 'rb'): > > print line > > > > inp = open('file.dcm', 'rb') > > print inp.readlines() > > > > Second, I have never tried to parse a binary file. I could use a few > > hints to get started. > > > > Bryan > > > > -- > > "The game of science can accurately be described as a never-ending > > insult to human intelligence." - João Magueijo > > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] upper and lower case input for file name
Is there an easy way that an input can be upper or lower case? The file name is TEST.TXT, and I get. - Enter File (if not local, enter path):test.txt Traceback (most recent call last): File "test.py", line 52, in for line in open(file) : IOError: [Errno 2] No such file or directory: 'test.txt' - This is a non-issue on Windows, but now I have migrated to Ubuntu. Bryan -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] identifying and parsing string in text file
I have a large file that has many lines like this, SITE I would like to identify the line by the tag (300a,0014) and then grab the name (DoseReferenceStructureType) and value (SITE). I would like to create a file that would have the structure, DoseReferenceStructureType = Site ... ... Also, there is a possibility that there are multiple lines with the same tag, but different values. These all need to be recorded. So far, I have a little bit of code to look at everything that is available, for line in open(str(sys.argv[1])): i_line = line.split() if i_line: if i_line[0] == "___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing DICOMRT file
Haven't had a chance to look at this in a while. On Wed, Dec 12, 2007 at 6:57 PM, John Fouhy <[EMAIL PROTECTED]> wrote: > On 13/12/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > I am new to doing anything like this. I have looked at > > http://www.leadtools.com/SDK/Medical/DICOM/ltdc1.htm and am > > not sure how to proceed. > > I haven't much experience here, but this is how I'd proceed, I think: > > 1. Start by reading the file. It's binary data (I guess) so there's > no point in reading lines.: > rawData = open('file.dcm', 'rb').read() > > 2. Write a function to parse the preamble: > > def parsePreamble(data): >preamble = data[:128] >dicm = data[128:132] > ># you might need to read up on encodings and things to make sure > this test is valid >if dicm == 'DICM': > return preamble, 132 >else: > raise NotAPreambleException This satisfies the if statement. > > > 3. Write functions to parse data elements. The functions are going to > try to parse a data element starting at a particular position, and if > successful, return the position of the end of the element. > > def parseDataelement(data, start): ># do stuff -- the web page you linked didn't have enough information > here >return element, pos I would like to extract 10-20 values from the file. Starting at byte 132, the data elements are specified in the Explicit VR little endian transfer syntax with a group number of 0002. The data element (0002, 0010) contains the Transfer Syntax UID, which specifies how the data elements following the file meta information are encoded. For this one, it is 1.2.840.10008.1.2 which is equal to LittleEndianImplicit. where there is the 2-byte group number, a 2-byte element number, a 4-byte value length (VL) field, and a value field containing VL bytes. Could someone help me get started. I did an xml dump with another program and got, PHOTON as an output example. > > > 4. Parse the whole thing; > > def parseDICOM(data): >elements = [] >try: > preamble, next = parsePreamble(data) >except NotAPreambleException: > preamble, next = None, 0 > >while True: > element, next = parseDataElement(data, next) > elements.append(element) > # you will need some way of breaking out of this loop, either by > checking the structure of > # element for an end condition, or by parseDataElement raising > an exception. > >return elements # and maybe preamble too if you want it > > HTH! > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem with logic while extracting data from binary file
Here is my program. I am trying to extract values from a binary file for use in a calculation. I am having trouble with the logic. Everything goes well until I use the parseSequence function. If there is only one sequence I seem fine, but if there is a sequence within a sequence everything seems to fall apart. I don' t know if I have just looked at this so long that I am not seeing the obvious fix. Any help would be appreciated. I have attached the input file that I am using for a test. Also, here is one of the strings that enters the parseSequence function, broken up to make it more readable. I would like to get parseDICOM(rawData)['\n0\x82']. - ('\n0', 'p\x00', 544, '\xfe\xff\x00\xe0\x18\x02\x00\x00 \n0q\x00\x02\x00\x00\x001 \n0x\x00\x02\x00\x00\x0010 \n0\x80\x00\x02\x00\x00\x004 \n0\xa0\x00\x02\x00\x00\x000 \x0c0\x04\x00\xe8\x01\x00\x00 \xfe\xff\x00\xe0p\x00\x00\x00 *\n0\x82*\x002\x00\x00\x0042.9068704277562\\- 392.3545926477\\189.182112099444 \n0\x84\x00\x0c\x00\x00\x008.9617062e-1 \n0\x86\x00\x10\x00\x00\x00127.378510918301 \x0c0\x06\x00\x02\x00\x00\x001 \xfe\xff\x00\xe0p\x00\x00\x00 *\n0\x82*\x002\x00\x00\x0042.9068704277562\\- 392.3545926477\\189.182112099444 \n0\x84\x00\x0c\x00\x00\x001.629998e-1 \n0\x86\x00\x10\x00\x00\x0023.159729257873 \x0c0\x06\x00\x02\x00\x00\x004 \xfe\xff\x00\xe0t\x00\x00\x00 *\n0\x82*\x002\x00\x00\x0042.9068704277562\\- 392.3545926477\\189.182112099444 \n0\x84\x00\x10\x00\x00\x001.26285318894435 \n0\x86\x00\x10\x00\x00\x00227.690980638769 \x0c0\x06\x00\x02\x00\x00\x003 \xfe\xff\x00\xe0t\x00\x00\x00 \n0\x82\x002\x00\x00\x0042.9068704277562\\- 392.3545926477\\189.182112099444 \n0\x84\x00\x10\x00\x00\x001.52797639111557 \n0\x86\x00\x10\x00\x00\x00263.433384670643 \x0c0\x06\x00\x02\x00\x00\x002 ') import struct from time import * print "Creating Variables...\n" start=clock() rawData = open('file.dcm', 'rb').read() # Need to get Transfer Syntax UID (0002,0010) for encoding type def parsePreamble(data): preamble = data[:128] dicm = data[128:132] if dicm == 'DICM': return preamble, 132 else: raise NotAPreambleException def parseMetaElementGL(data): vl_field = data[138:140] length = struct.unpack('h', vl_field)[0] value = struct.unpack('hh',data[140:(140+length)])[0] return value def parseDataElement(data, start): if start < (144+parseMetaElementGL(rawData)): group_num = data[start:start+2] element_num = data[start+2:start+4] vr_field = data[start+4:start+6] if vr_field == 'UN' or vr_field == 'SQ' or vr_field == 'OB' or vr_field == 'OW': unused = data[start+6:start+8] vl_field = data[start+8:start+12] length = struct.unpack('hh', vl_field)[0] # 4-byte value = data[start+12:(start+12+length)] pos = start+12+length element = (group_num+element_num) return element, pos, value, length else: vl_field = data[start+6:start+8] length = struct.unpack('h', vl_field)[0]# 2-byte value = data[start+8:(start+8+length)] pos = start+8+length element = (group_num+element_num) return element, pos, value, length else: while start < len(data): group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] pos = start+8+length element = (group_num+element_num) return element, pos, value, length else: print "End of File" def parseSequence(data, start): group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] pos = start+8+length element = (group_num+element_num) if element == '\xfe\xff\x00\xe0': start = start+8 group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] pos = start+8+length element = (group_num+element_num) if element == '\xfe\xff\x00\xe0': start = start+8 group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length =
Re: [Tutor] Problem with logic while extracting data from binary file
the start_2 is supposed to be start On Thu, Mar 27, 2008 at 5:42 PM, Bryan Fodness <[EMAIL PROTECTED]> wrote: > Thanks again, > > I can't seem to keep track of my start values when I break up the value > variable into svalues. Do you think I should do this, or should I have a > running count from the beginning of the file and keep track until the end? > > I am trying to find \n0\x82\x00 and \n0\x84\x00 within the block. I've > added the if statement, and it seems to enter the parseSequence function the > way I would expect, but it does not seem to populate the dictionary. > > Sorry for so many questions about this, but I feel like I am so close. > > > while next < len(data): > for element, next, value, length in parseDataElement(data, next): > ##if element in ('\n0\x10\x00', '[EMAIL PROTECTED]', '\n0p\x00', > ## '\n0\xb0\x00', '\n0\x80\x01'): > if element == '\n0p\x00': > start = 0 > while start < length: > element, start, svalue = parseSequence(value, start) > if svalue.startswith('\xfe\xff\x00\xe0'): > start_2 = 0 > element, start_2, svalue = parseSequence(svalue, > start) > search[element].append(svalue) > search[element].append(svalue) > else: > search[element].append(value) > return search > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with logic while extracting data from binary file
Thanks again, I can't seem to keep track of my start values when I break up the value variable into svalues. Do you think I should do this, or should I have a running count from the beginning of the file and keep track until the end? I am trying to find \n0\x82\x00 and \n0\x84\x00 within the block. I've added the if statement, and it seems to enter the parseSequence function the way I would expect, but it does not seem to populate the dictionary. Sorry for so many questions about this, but I feel like I am so close. while next < len(data): for element, next, value, length in parseDataElement(data, next): ##if element in ('\n0\x10\x00', '[EMAIL PROTECTED]', '\n0p\x00', ## '\n0\xb0\x00', '\n0\x80\x01'): if element == '\n0p\x00': start = 0 while start < length: element, start, svalue = parseSequence(value, start) if svalue.startswith('\xfe\xff\x00\xe0'): start_2 = 0 element, start_2, svalue = parseSequence(svalue, start) search[element].append(svalue) search[element].append(svalue) else: search[element].append(value) return search ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with logic while extracting data from binary file
On Thu, Mar 27, 2008 at 6:43 PM, bob gailer <[EMAIL PROTECTED]> wrote: > Bryan Fodness wrote: > > Thanks again, > > The problem is that parseSequence gets the length of the block, then, in > effect, skips to the next block. That bypasses the sub-sequences you want. > > parseSquence needs to examine the block for the sub-blocks beginning > with \n0\x84\x00. This will probably require another while loop. I will try that. > > > Do you use a debugger? It has been very helpful to me in tracking down > the problem. What operating system are you running? What IDE or > development tool are you using? I have not used a debugger yet, I was getting ready to try that. I am using IDLE on Windows Vista. > > > [snip] > > -- > Bob Gailer > 919-636-4239 Chapel Hill, NC > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with logic while extracting data from binary file
Thanks again, Still lost, even with watching the variables. I see that it kicks out of the loop, but don't understand what I have done to cause this. I'm sorry for repeated emails, but I have spent multiple days on this. I have added another while loop that I think should work, but I can't seem to keep it in the while loop. I feel like I am getting close. It seems like it gets everything at the first level , but not the way I expected. It seems to get the first value inside the first while loop, and then goes outside the loop to get the next three. I would have expected it to return the values as it goes through the first while loop (since they are at the same level), then when it sees the nested identifier, go into the second while loop and return values. Any insight would be wonderful. def parseSequence(data, start): group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] pos = start+8+length element = (group_num+element_num) if element == '\xfe\xff\x00\xe0': data = value while start < 536: #length: # 536 group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] start = start+8+length element = (group_num+element_num) if element == '\xfe\xff\x00\xe0': data = value while start < 112: #length: # 112, 112, 116, 116 group_num = data[start:start+2] element_num = data[start+2:start+4] vl_field = data[start+4:start+8] length = struct.unpack('hh', vl_field)[0] value = data[start+8:(start+8+length)] start = start+8+length element = (group_num+element_num) return element, start, value else: return element, start, value else: return element, pos, value ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using split with a backslash
I have a data pair separated by a backslash. I didn' t think it would see an end of line if the backslash was inside the quotes. Can this be done? I don't have a choice in what the separator is. >>> LeafJawPositions='-42.0001\29.8001' >>> LeafJawPositions '-42.0001\x029.8001' >>> x1, x2 = LeafJawPositions.split('\x0') ValueError: invalid \x escape >>> x1, x2 = LeafJawPositions.split('\') SyntaxError: EOL while scanning single-quoted string >>> -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using split with a backslash
Thanks everyone, I was trying it this way. x1, x2 = LeafJawPositions.split(r'\\') On Wed, Apr 2, 2008 at 11:00 AM, Michael Connors <[EMAIL PROTECTED]> wrote: > > >>> LeafJawPositions='-42.0001\29.8001' > > >>> LeafJawPositions > > '-42.0001\x029.8001' > > >>> x1, x2 = LeafJawPositions.split('\x0') > > ValueError: invalid \x escape > > >>> x1, x2 = LeafJawPositions.split('\') > > > > SyntaxError: EOL while scanning single-quoted string > > >>> > > > > Hi, > The backslash is used for escaping special characters in a string. In > order to do what you are trying to do, you would need to escape the > backslash using a backslash. > You need to do this in two places in the above code. > LeafJawPositions='-42.0001\\29.8001' > > and > > x1, x2 = LeafJawPositions.split('\\') > > http://docs.python.org/ref/strings.html > > Regards, > Michael > > -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] string from input file
I am trying to get values from an input file, 0.192 Custom 15 IN but, when I check to see if they are equal it is not true. f = open(infile, 'r') s = f.readlines() f.close() Block = str(s[1]) Angle = float(s[2]) Position = str(s[3]) if Block == 'Custom': print 'equal' if I print Block+'Custom', I get, Custom Custom when I would have expected CustomCustom Can someone help me figure out if I am not reading the values in correctly, is there a control character at the end? The float value is ok. -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] best way to get external data
I am trying to figure out the best way to get external data. Using the following data in a file 1 2 3 I have used, fi = open(infile, 'r') s = fi.readlines() fi.close() a = s[0] b = s[1] c = s[2] but, if I have, x = 1 y = 2 z = 3 I have used, for line in open(infile): i_line = line.split() if i_line[0] == 'x': a = i_line[2] elif i_line[0] == 'y': b = i_line[2] elif i_line[0] == 'z': c = i_line[2] I do have control over the creation of the data file. The second way is better in my mind, because it is independent of the ordering. If I have a long list of 'variable' = 'value', could this be better managed in a dictionary? Or, is there a better way? Thanks -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] reading a string into an array
I am trying to read a long string of values separated by a backslash into an array of known size. Here is my attempt, from numpy import * ay, ax = 384, 512 a = zeros([ay, ax]) for line in open('out.out'): data = line.split('\\') k = 0 for i in range(ay): for j in range(ax): a[i, j] = data[k] k+=1 but, I receive the following error. Traceback (most recent call last): File "C:/Users/bryan/Desktop/dose_array.py", line 13, in a[i, j] = data[k] ValueError: setting an array element with a sequence. -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] trying to change the number of elements in array while preserving data
I tried posting this to numpy, but my posts never show up. So, I was hoping someone here might be able to help me. I have two arrays that are different sizes and i would like to be able to add them for plotting. If I have an array a and b, [[1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9]] [[0 0 0 0 0] [0 3 3 3 0] [0 3 3 3 0] [0 3 3 3 0] [0 0 0 0 0]] but I would like to change b to look like this, [[0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 3 3 3 3 3 0 0] [0 0 3 3 3 3 3 0 0] [0 0 3 3 3 3 3 0 0] [0 0 3 3 3 3 3 0 0] [0 0 3 3 3 3 3 0 0] [0 0 3 3 3 3 3 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0]] so I can get the sum of a and b. My data will not be regular like these. I have a 400x60 array with irregular data that I would like as a 400x400 array. Does anybody know of an easy way to accomplish this? Bryan -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Posting to Numpy and Scipy
Has anyone had a problem posting to either of these mailing lists. I am a member and have sent a few posts to each of them over the last couple months, but none of them show up in the list. I always receive a 'awaiting moderator approval' email. I have sent an email to the owner about this, but have not received a response. -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. " -Albert Einstein ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] my own error code when no argument is sent
when i execute my program without an argument i receive, infile = sys.argv[1] IndexError: list index out of range is there a way that i could suppress this and add my own error code ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] manipulating a string
I have a string (column names) that I need to split. D_H = 'D 5 10 15 20 25 30 35 40 D Upper D Lower' I cannot do a simple list(D_H).split because the last two strings have a space between them (D Upper and D Lower are two, not four labels). Any suggestions? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] accessing string in list
I have a list of labels for a data file, test = ['depth', '4', '6', '10', '15', '20', '30', '40', 'angle'] I would like to get the string of the first value that is greater than a known value and the previous string. If I have 15.8, I would like to get the index of '20' and '15'. I would also like to make sure that my known value falls in the range 4-40. I am having trouble with the mixed types. for i in range(len(test)): if Field < int(test[i]): print i ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] checking if data files are good, readable, and exist
I would like to check to see if the data files are good, readable, and exist. I have checked to see if they exist, but their is a possibility that the data file might be binary, and I would like to have a sys.exit for that as well. if not os.path.isfile(A_data) or not os.path.isfile(B_data)\ or not os.path.isfile(C_data) or not os.path.isfile(D_data): sys.exit(14) -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] checking for expected types from input file
I am populating a dictionary from an input file, and would like to create an error code if a string is sent to a variable that expects a float or int. INPUT = {} for line in open(infile): input_line = line.split(' = ') INPUT[input_line[0].strip()] = input_line[1].strip() if INPUT.has_key('ReferencePositionX'): refx = float(INPUT['ReferencePositionX'])/10 refy = float(INPUT['ReferencePositionY'])/10 refz = float(INPUT['ReferencePositionZ'])/10 I have many variables of different types, and I want to do a check in case something like ReferencePositionX = abc occurs. -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] iterating data and populating a dictionary
i am filling a dictionary with a dictionary and my values for isegment[field] are identical. i can't see where i am overwriting the previous field values. my data looks like Field = aa1 Index = 0.0 Value = 0.0 ... ... Field = aa2 Index = 0.01 Value = 0.5 ... i would like to have something like, {1: {'Value': 0.0, ...}, 2: {'Value': 0.01, ...}} for line in file(data_file): the_line = line.split() if the_line: if the_line[0] == 'Field': field += 1 elif the_line[0] == 'Index': index = float(the_line[-1]) dif_index = index - start_index iindex[field] = dif_index start_index = index elif the_line[0] == 'Value': segment[the_line[1]] = float(the_line[-1])*(ax/40) isegment[field] = segment -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] accessing list from a string
I have a list in a text file that is in the python format., Positions = [2.5,2.8] and would like to grab the values. for line in file('list.txt'): if line == Positions: x1,x2=Positions I know this does not work. Is there a direct way to get my x1 and x2 values. Thanks, Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] try except block for multiple statements
I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write("a = %s\n" %plan.a) fo.write("b = %s\n" %plan.b) fo.write("c = %s\n" %plan.c) fo.write("d = %s\n" %plan.d) except AttributeError: pass -- "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. " -Albert Einstein ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] no attribute
I am trying to change values in a file. The following code does not seem to find the attribute. def anonymize(obj, attr): try: obj.attr = 'Anonymize' except AttributeError: pass for fname in os.listdir(os.curdir): plan=ReadFile(fname) anonymize(plan, 'Name') It seems to be the obj.attr in the function. Can someone explain what I am doing wrong? Bryan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Script to take a screenshot of my website.
I would like to take a screenshot of my website without opening the browser or importing extra packages. Is there a way to do this? I would like to create a function like screenshot(' http://www.scatterworks.com";) -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor