shutil ignore fails on passing a tuple?
This beats me: ipatterns ('*.txt', '*.hdf', '*.pdf', '*.png') igf = shutil.ignore_patterns(ipatterns) ignorethis = igf(ddftopdir,os.listdir(ddftopdir)) Traceback (most recent call last): File "", line 1, in ignorethis = igf(ddftopdir,os.listdir(ddftopdir)) File "C:\Python27\lib\shutil.py", line 138, in _ignore_patterns ignored_names.extend(fnmatch.filter(names, pattern)) File "C:\Python27\lib\fnmatch.py", line 49, in filter pat=os.path.normcase(pat) File "C:\Python27\lib\ntpath.py", line 46, in normcase return s.replace("/", "\\").lower() AttributeError: 'tuple' object has no attribute 'replace' igg = shutil.ignore_patterns('*.txt', '*.hdf', '*.pdf', '*.png') ignorethat = igg(ddftopdir, os.listdir(ddftopdir)) ignorethat set(['Chi2.png', 'DTSdata.hdf', 'TST.hdf', 'BullNoseDiffs.png', 'DTSall.hdf', 'Symmetry.pdf']) Why does it fail on passing in a tuple of ignore strings? I thought the , (comma) is pretty much the tuple constructor (if that is the right word). How can I solve this? Is there a way to convert a tuple of strings in a form that will be accepted? Thank you in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
logging time format millisecond precision
I use this formatter in logging: formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t %(levelname)s \t %(message)s') Sample output: 2012-07-19 21:34:58,382 root INFO Removed - C:\Users\ZDoor\Documents The time stamp has millisecond precision but the decimal separator is a comma. Can I change the comma (,) into a period (.) and if so how? Thanks in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
logging time format millisecond precision decimalsign
I use this formatter in logging: formatter = logging.Formatter(fmt='%(asctime)s \t %(name)s \t %(levelname)s \t %(message)s') Sample output: 2012-07-19 21:34:58,382 root INFO Removed - C:\Users\ZDoor\Documents The time stamp has millisecond precision but the decimal separator is a comma. Can I change the comma (,) into a period (.) and if so how? Thanks in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Difference between tempfile and spooled tempfile?
I do not understand why the spooled write gives an error. See below. The normal tempfile works just fine. They are supposed to behave equal? All insight you can provide is welcome. Alex van der Spek + Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. import array import tempfile stf = tempfile.SpooledTemporaryFile(max_size=1024) ptf = tempfile.TemporaryFile() fff = [float(x) for x in range(2048)] ffa = array.array('f',fff) ptf.write(ffa) stf.write(ffa) Traceback (most recent call last): File "", line 1, in stf.write(ffa) File "C:\Python27\lib\tempfile.py", line 595, in write rv = file.write(s) TypeError: must be string or read-only character buffer, not array.array -- http://mail.python.org/mailman/listinfo/python-list
Re: Plot a function with matplotlib?
On Sat, 19 May 2012 01:59:59 +, Steven D'Aprano wrote: > I have matplotlib and iPython, and want to plot a function over an > equally-spaced range of points. > > That is to say, I want to say something like this: > > plot(func, start, end) > > rather than generating the X and Y values by hand, and plotting a > scatter graph. All the examples I've seen look something like this: > > from pylab import * > import numpy as np > t = arange(0.0, 2.0+0.01, 0.01) # generate x-values s = sin(t*pi) # > and y-values > plot(t, s) > show() > > > which is fine for what it is, but I'm looking for an interface closer to > what my HP graphing calculator would use, i.e. something like this: > > > plot(lambda x: sin(x*pi), # function or expression to plot, > start=0.0, > end=2.0, > ) > > and have step size taken either from some default, or better still, > automatically calculated so one point is calculated per pixel. > > Is there a way to do this in iPython or matplotlib? Not to my knowledge unless you code it yourself. However in gnuplot (www.gnuplot.info) gnuplot>>> set xrange[start:end] gnuplot>>> foo(x)=mycomplicatedfunction(x) gnuplot>>> plot foo(x) or shorter still gnuplot>>> plot [start:end] foo(x) without the need to set the xrange in advance. -- http://mail.python.org/mailman/listinfo/python-list
Text file with mixed end-of-line terminations
I have a text file that uses both '\r' and '\r\n' end-of-line terminations. The '\r' terminates the first 25 lines or so, the remainder is termiated with '\r\n' Reading this file like this: for line in open(filename,'r'): line= #Do whatever needs doing... The first line read is actually a string consiting of the first 25 lines. The readline() method does the same thing. Is there a way to make it read one line at a time, regardless of the line termination? By the way, the newlines attribute reports None after reading a few lines. I tried on Linux and Windows. I use the standard binaries as distributed. Thanks in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
HDF5 tree walker
Is there an equivalent to os.path.walk() for HDF5 file trees accessed through h5py? Thanks! Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Sending keystrokes to Windows exe programs
I can start a windows program on Vista with: import subprocess dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE) Unfortunately sending keystrokes with communicate() does not appear to work: dva.communicate('F2') this does not produce any result but it does make IDLE become really idle. >>> dva.terminate() however does work fine and kills the program as it should. Is there a way or do I have to go back to Visual Basic? Regards, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Appending to dictionary of lists
I open a csv file and create a DictReader object. Subsequently, reading lines from this file I try to update a dictionary of lists: csvf=open(os.path.join(root,fcsv),'rb') csvr=csv.DictReader(csvf) refd=dict.fromkeys(csvr.fieldnames,[]) for row in csvr: for (k,v) in row.items(): refd[k].append(v) I do not understand why this appends v to every key k each time. Thanks in advance for any tips you can pass on. Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: Appending to dictionary of lists
Thank you! Would never have found that by myself. "Paul Rubin" wrote in message news:7x7ha75zib@ruckus.brouhaha.com... "Alex van der Spek" writes: refd=dict.fromkeys(csvr.fieldnames,[]) ... I do not understand why this appends v to every key k each time. You have initialized every element of refd to the same list. Try refd = dict((k,[]) for k in csvr.fieldnames) instead. -- http://mail.python.org/mailman/listinfo/python-list
Tkinter FileDialog
Trying to bring up a simple File or Directory chooser using Tkinter. Right now I use pywin32 as Mark Hammond's extensions are somewhat more familiar to an (ex)VB programmer who started way back with Fortran. On Windows Vista with Python 2.7. The examples from Mark Lutz' book "Programming Python" do not immediately apply. The book describes Python 3.x only. This is what I do (in IDLE): import tkFileDialog as tk file2open=tk.Open().show() This works. But it also brings up a persistent Tk window. Killing this window forces a restart in IDLE, which is not desirable. There does not appear to be a way to multiselect files? I am probably missing a whole lot. Please point me to a simple example of GUI file manipulations. I can save myself with os.path.walk and os.listdir but the users I try to serve cannot. Regards, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Multiple file select with tkFileDialog passes back 'decorated' strings (sometimes)
I switched from Mark Hammonds pywin32 extensions for file choosers as the multiselect there seems to crash on me when selecting more than a few dozen. Using Tk now. Works well but the resulting string passed back seems to 'decorated' when the files are on local disk and not decorated when retrieved over a USB interface from an external disk? I do this: From local disk I get back: '{file1.bin} {file2.bin}' From external disk I get back: 'file1.bin file2.bin' I can handle/parse both, not an issue but it raises the question: Are these the only two possibilities? Is it the same across platforms (I use Python 2.7 on Win Vista)? See code below. Thanks for the insight! Alex van der Spek +++ from Tkinter import * import tkFileDialog as tkf tkroot=Tk() tkroot.withdraw() initdir=os.environ['HOME'] filetype=[('Binary Timeseries','*.bin'),('All files','*.*')] filenames=tkf.askopenfilenames(parent=tkroot,initialdir=initdir,filetypes=filetype) tkroot.destroy() -- http://mail.python.org/mailman/listinfo/python-list
Deep vs. shallow copy?
I think I understand the difference between deep vs. shallow copies but I was bitten by this: with open(os.path.join('path', 'foo.txt', 'rb') as txt: reader = csv.reader(txt) data = [row.append(year) for row in reader] This does not work although the append does complete. The below works: with open(os.path.join('path', 'foo.txt', 'rb') as txt: reader = csv.reader(txt) data = [row + [year] for row in reader] However in this context I am baffled. If someone can explain what is going on here, I would be most grateful. Alex van der Spek -- https://mail.python.org/mailman/listinfo/python-list
Re: Deep vs. shallow copy?
On Wed, 12 Mar 2014 10:00:09 -0500, Zachary Ware wrote: > On Wed, Mar 12, 2014 at 9:25 AM, Alex van der Spek > wrote: >> I think I understand the difference between deep vs. shallow copies but >> I was bitten by this: >> >> with open(os.path.join('path', 'foo.txt', 'rb') as txt: >> reader = csv.reader(txt) >> data = [row.append(year) for row in reader] >> >> This does not work although the append does complete. The below works: >> >> with open(os.path.join('path', 'foo.txt', 'rb') as txt: >> reader = csv.reader(txt) >> data = [row + [year] for row in reader] >> >> However in this context I am baffled. If someone can explain what is >> going on here, I would be most grateful. > > Deep/shallow copying doesn't really come into this. row.append() > mutates the list (row), it doesn't return a new list. Like most > in-place/mutating methods in Python, it returns None instead of self to > show that mutation was done, so your listcomp fills `data` with Nones; > there is no copying done at all. The second example works as you > expected because `row + [year]` results in a new list, which the > listcomp is happy to append to `data`--which does mean that `row` is > copied. > > To avoid the copy that the second listcomp is doing (which really > shouldn't be necessary anyway, unless your rows are astronomically > huge), you have a couple of options. First, you can expand your > listcomp and use append: > >with open(os.path.join('path', 'foo.txt'), 'rb') as txt: # with > your typo fixed ;) >reader = csv.reader(txt) >data = [] >for row in reader: >row.append(year) >data.append(row) > > To me, that's pretty readable and pretty clear about what it's doing. > Then there's this option, which I don't recommend: > >import operator >with open(os.path.join('path', 'foo.txt'), 'rb') as txt: >reader = csv.reader(txt) >data = [operator.iadd(row, [year]) for row in reader] > > This works because operator.iadd is basically shorthand for > row.__iadd__([year]), which does return self (otherwise, the assignment > part of `row += [year]` couldn't work). But, it's not as clear about > what's happening, and only saves a whole two lines (maybe 3 if you > already have operator imported). > > Hope this helps, Thank you, that helped immensely! Having been taught programming in Algol60 Python still defeats me at times! Particularly since Algol60 wasn't very long lived and what came thereafter (FORTRAN) much worse. I get it now, the below is equivalent! I am perfectly happy with the one copy of the list row + [year]. Just wanted to learn something here and I have! Python 2.6.5 (r265:79063, Feb 27 2014, 19:44:14) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = [1,2,3] >>> b = 'val' >>> a.append(b) >>> a [1, 2, 3, 'val'] >>> c = a.append(b) >>> print c None >>> -- https://mail.python.org/mailman/listinfo/python-list
ctypes: returning an array from a subroutine
I have a C code function like this: ++ int __declspec(dllexport) __stdcall bnd2prb(float *in, float *out, int init) {enum {OK, Error, Not_Valid}; ... return(OK): } ++ And in Python I am trying to call this C function: ++ import ctypes import struct import array _snns = ctypes.windll.LoadLibrary(r'C:\MSDEV\Projects\Cyctrac\Debug\Cyctrac.dll') _cosm = getattr(_snns, '_bnd2prb@12') _cosm.argtypes = (ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_long) _cosm.restype = ctypes.c_float def snns(indata, outdat): """Calls the neural net, returns a vector of 4. """ global _cosm init = ctypes.c_long(0) ilen = len(indata) olen = len(outdat) itype = ctypes.c_float * ilen otype = ctypes.c_float * olen iok = _cosm(itype(*indata), ctypes.pointer(otype(*outdat)), init) if not iok: return True else: return False indata = [0.5 for x in range(31)] outdat = [0.0 for x in range(4)] indata[1]=3.14 ok = snns(indata, outdat) if ok: print indata print outdat +++ This executes but leaves the outdat array unchanged. Obviously I haven't understood ctypes well enough. Returning arrays from FORTRAN I routinely do through a string_buffer. That works very well but did not work here at all. Any and all help welcome. Alex van der Spek -- https://mail.python.org/mailman/listinfo/python-list
Re: ctypes: returning an array from a subroutine
Many hours later I found a working solutions in ctypes: The below makes sense to me but I am still at a loss why the first solution did not work. Anybody willing to explain for my better understanding? Regards, Alex van der Spek _snns = ctypes.windll.LoadLibrary(r'C:\MSDEV\Projects\Cyctrac\Debug\Cyctrac.dll') _cosm = getattr(_snns, '_bnd2prb@12') _cosm.restype = ctypes.c_int def snns(indata, outdat): """Calls the neural net, returns a vector of 4. """ global _cosm init = ctypes.c_long(0) odat = (ctypes.c_float * len(outdat))(*outdat) idat = (ctypes.c_float * len(indata))(*indata) iok = _cosm(ctypes.byref(idat), ctypes.byref(odat), init) for i, x in enumerate(odat): outdat[i] = x if not iok: return odat else: return False indata = [0.5 for x in range(31)] outdat = [0.0 for x in range(4)] ok = snns(indata, outdat) if ok: print indata print outdat +++++++ "Alex van der Spek" wrote in message news:5353bf06$0$2830$e4fe5...@news2.news.xs4all.nl... I have a C code function like this: ++ int __declspec(dllexport) __stdcall bnd2prb(float *in, float *out, int init) {enum {OK, Error, Not_Valid}; ... return(OK): } ++ And in Python I am trying to call this C function: ++ import ctypes import struct import array _snns = ctypes.windll.LoadLibrary(r'C:\MSDEV\Projects\Cyctrac\Debug\Cyctrac.dll') _cosm = getattr(_snns, '_bnd2prb@12') _cosm.argtypes = (ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_long) _cosm.restype = ctypes.c_float def snns(indata, outdat): """Calls the neural net, returns a vector of 4. """ global _cosm init = ctypes.c_long(0) ilen = len(indata) olen = len(outdat) itype = ctypes.c_float * ilen otype = ctypes.c_float * olen iok = _cosm(itype(*indata), ctypes.pointer(otype(*outdat)), init) if not iok: return True else: return False indata = [0.5 for x in range(31)] outdat = [0.0 for x in range(4)] indata[1]=3.14 ok = snns(indata, outdat) if ok: print indata print outdat +++ This executes but leaves the outdat array unchanged. Obviously I haven't understood ctypes well enough. Returning arrays from FORTRAN I routinely do through a string_buffer. That works very well but did not work here at all. Any and all help welcome. Alex van der Spek -- https://mail.python.org/mailman/listinfo/python-list
dict to boolean expression, how to?
With a dict like so: cond = {'a': 1, 'b': 1, 'c': 1, 'A': 0, 'B', 0, 'C':0} how would you make a boolean expression like this: bool = (('a' == 1) & ('A' == 0) | ('b' == 1) & ('B' == 0) | ('c' == 1) & ('C' == 0)) The fact that lowercase and uppercase keys are stringed together with & is intentional albeit the actual condition is a bit more tricky. I've tried several approaches using eval() on a string built from the dict but landed with just spelling it out literally. Any pointers welcome. Alex -- https://mail.python.org/mailman/listinfo/python-list
Re: dict to boolean expression, how to?
On Fri, 01 Aug 2014 12:45:12 +, Alex van der Spek wrote: > With a dict like so: > > cond = {'a': 1, 'b': 1, 'c': 1, > 'A': 0, 'B', 0, 'C':0} > > how would you make a boolean expression like this: > > bool = (('a' == 1) & ('A' == 0) | > ('b' == 1) & ('B' == 0) | > ('c' == 1) & ('C' == 0)) > > The fact that lowercase and uppercase keys are stringed together with & > is intentional albeit the actual condition is a bit more tricky. > > I've tried several approaches using eval() on a string built from the > dict but landed with just spelling it out literally. > > > Any pointers welcome. > Alex I am sorry, the problem is ill posed. 'a', 'A' and so forth are my failed attempt to shorthand. In reality the dict's keys are column names in a pandas dataframe df. The boolean expression would therefore look like: bool = ((df['a'] == 1) & (df['A'] == 0) | (df['b'] == 1) & (df['B'] == 0) | (df['c'] == 1) & (df['C'] == 0)) I do know eval() lends itself to code injection but can't say I am fully aware of its dangers. It seemed like a handy tool to me. This newsgroup scares me, it appears to be for professional computer scientists only, the theoretical part is sometimes too much for this practical physicist with an old background in FORTRAN. Is there a better place to ask questions of this nature? Alex van der Spek -- https://mail.python.org/mailman/listinfo/python-list
Numpy.array with dtype works on list of tuples not on list of lists?
Why does this not work? dat=[[1,2,3],[4,5,6]] col=[('a','f4'),('b','f4'),('c','f4')] arr=numpy.array(dat,dtype=col) Traceback (most recent call last): File "", line 1, in arr=numpy.array(dat,dtype=col) TypeError: expected a readable buffer object But this does: dat=[(1,2,3),(4,5,6)] arr=numpy.array(dat,dtype=col) arr array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', ' The only difference that the object is a list of tuples now? Thanks for clarification, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
xml tree writing with ElementTree; prepends elements with ns0
When reading a tree and writing it back to a new file all the elements are prepended with the string ns0: Why is it prepended and how can I suppress this? Thanks, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
WAVE file writing, confused about setsampwidth(n)
I am confused about the Wave_write object setsampwidth(n). Is the sample width n the total sample width, i.e. for a stereo sample consisting of short (2 byte) integers; n=4 or is the sample width the number of bytes in either the left or the right channel? Regards, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: Using SciPy in application
On Wed, 24 Apr 2013 04:34:44 -0700, Roozbeh wrote: The scipy interpolation routines (splev, splrep, etc.) are on netlib: http://www.netlib.org/dierckx/ This gives you FORTRAN source codes which you will have to compile yourself to either a DLL or an SO. Call them from python using ctypes. I have done that for a number of the dierckx routines. No problems at all. Hope this helps, Alex van der Spek > On Wednesday, April 24, 2013 11:13:45 AM UTC+2, Roozbeh wrote: >> Hi all, I want to use spline interpolation function from SciPy in an >> application and at the same time, I don't want the end user to have to >> install SciPy separately. Is there a way around this problem? Thanks in >> advance for your help > > Any idea where can I find the recipe for the spline interpolation that > does not rely on NumPy and/or SciPy and is written pure Python (no C > code)? -- http://mail.python.org/mailman/listinfo/python-list
IDLE/Python on Asus EEE PC
Simple Python programs edited and run through IDLE work fine on my Ubuntu Linux system without any editing. However on my Asus EEE PC IDLE complains about incorrect formatting (indentation) or possibly mixing tabs/spaces. The source code is exactly the same. There is no incorrect formatting and certainly no use of tabs. I created the program on my EEE, was unable to find anything wrong with it and decided to test it on my desktop. I was amazed that it runs fine there. Can anyone explain this? I use the full desktop Xandros OS on the EEE. I downloaded IDLE from the Debian repositories. Thanks in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
eval('07') works, eval('08') fails, why?
I am baffled by this: IDLE 1.2.2 No Subprocess >>> input() 07 7 >>> input() 08 Traceback (most recent call last): File "", line 1, in input() File "", line 1 08 ^ SyntaxError: invalid token of course, I can work around this using raw_input() but I want to understand why this happens. It boils down to: >>> eval('07') 7 >>> eval('08') Traceback (most recent call last): File "", line 1, in eval('08') File "", line 1 08 ^ SyntaxError: invalid token I can't think of anything that could cause this. Similarly, eval('09') fails, but for string 0x with x<8 it works. I am teaching myself Python in order to climb the ladder from Algol(1980s)-->Pascal(1990s)-- >VisualBasic(2000)-->Python. I am a physicist, have programmed computers all my life but I won't understand the real tech jargon of present day computer science. Please keep it simple Thanks in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Reading C# serialized objects into Python?
Is there a way to read C# serialized objects into Python? I know the definition and structure of the C# objects. The Python docs say that pickle is specific to Python, which does not give me much hope. There may be a library however that I haven't come across yet. Thanks much, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: eval('07') works, eval('08') fails, why?
Thanks much, that makes sense! Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Calling FORTAN dll functions from Python
Does anyone know how to call functions from FORTRAN dlls in Python? Is it even possible? I browsed the documentation for Python 2.6.1 and the Python/C API comes close to what I would like to do but it is strictly limited to C. Unfortunately the passing of arguments in C and FORTRAN is very different, not to mention the differences with strings where FORTRAN expects a hidden length argument. It could call the FORTRAN dll from C and call the C functions from Python but is that my only option? For reference: I am using Python 2.6.1 FORTRAN powerstation 4.0. It is not an option to translate the FORTRAN code to C (using f2c) as the source code is the official ASME version of calculating steam tables. I am interested in a solution that will work on Windows (XP and Vista) as well as Linux (Ubuntu 10.4) although the latter would not use dlls but code resources. I am a beginner in Python. The fact that I still use and can use FORTRAN gives away my age. Mixed language programming is not an issue for me (C/VB, VB/FORTRAN, C/FORTRAN) but Python is new. Just pointing me to relevant documentation would be helpful in its own right. Thank you in advance, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Opposite of split
Looking for a method that does the opposite of 'split', i.e. elements in a list are automatically concatenated with a user selectable spacer in between e.g. '\t'. This is to prepare lines to be written to a sequential file by 'write'. All hints welcome. Regards, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: Opposite of split
Thanks much, Nope, no homework. This was a serious question from a serious but perhaps simple physicist who grew up with Algol, FORTRAN and Pascal, taught himself VB(A) and is looking for a replacement of VB and finding that in Python. You can guess my age now. Most of my work I do in R nowadays but R is not flexible enough for some file manipulation operations. I use the book by Lutz ("Learning Python"). The join method for strings is in there. I did not have the book at hand and I was jetlagged too. I do apologize for asking a simple question. I had no idea that some would go to the extent of giving trick solutions for simple, supposedly homework questions. Bear in mind Python is a very feature rich language. You cannot expect all newbies to remember everything. By the way, I had a working program that did what I wanted using still simpler string concatenation. Replaced that now by tab.join([lines[i][k][2] for i in range(5)]), k being a loop counter. Judge for yourself. That is the level I am at after 6 weeks of doing excercises from my programming book on Pascal in Python. Thanks for the help. I do hope there is no entry level for using this group. If there is, I won't meet it for a while. Alex van der Spek "D'Arcy J.M. Cain" wrote in message news:mailman.2159.1281917130.1673.python-l...@python.org... On 15 Aug 2010 23:33:10 GMT Steven D'Aprano wrote: Under what possible circumstances would you prefer this code to the built- in str.join method? I assumed that it was a trap for someone asking for us to do his homework. I also thought that it was a waste of time because I knew that twenty people would jump in with the correct answer because of "finally, one that I can answer" syndrome. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
EOFError with fileinput
Using the fileinput module to process lists of files: for line in fileinput.input(logs): Unfortunately, EOFError does not seem to indicate the end-of-file condition correctly when using fileinput. How would you find the EOF file for all the files in the file list 'logs'? Thank you, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: Opposite of split
Perhaps the ones here who think I was trying to make you do my homework can actually help me for real. Since I run my own company (not working for any of the big ones) I can't afford official training in anything. So I teach myself, help is always welcome and sought for. If that feels like doing homework for me, so be it. The fact is that I do try to learn Python. It can do things I thought required much more coding. Look at the attached. It builds a concordance table first. That was an excercise from a book on Pascal programming. In Pascal the solution is 2 pages of code. In Python it is 8 lines. Beautiful! Anybody catches any other ways to improve my program (attached), you are most welcome. Help me learn, that is one of the objectives of this newsgroup, right? Or is it all about exchanging the next to impossible solution to the never to happen unreal world problems? Regards, Alex van der Spek "D'Arcy J.M. Cain" wrote in message news:mailman.2159.1281917130.1673.python-l...@python.org... On 15 Aug 2010 23:33:10 GMT Steven D'Aprano wrote: Under what possible circumstances would you prefer this code to the built- in str.join method? I assumed that it was a trap for someone asking for us to do his homework. I also thought that it was a waste of time because I knew that twenty people would jump in with the correct answer because of "finally, one that I can answer" syndrome. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. #! usr/bin/env python # Merge log files to autolog file import os import fileinput #top='C:\\Documents and Settings\\avanderspek\\My Documents\\CiDRAdata\\Syncrude\\CSL\\August2010' top='C:\\Users\\ZDoor\\Documents\\CiDRA\\Syncrude\CSL\\August2010' i,j,k=0,0,0 date={} fps=0.3048 tab='\t' bt='-999.25'+'\t''-999.25'+'\t''-999.25'+'\t''-999.25'+'\t'+'-999.25' al='Status'+'\t'+'State'+'\t'+'-999.25' for root,dirs,files in os.walk(top): #Build a concordance table of days on which data was collected for name in files: ext=name.split('.',1)[1] if ext=='txt': dat=name.split('_')[1].split('y')[1] if dat in date.keys(): date[dat]+=1 else: date[dat]=1 print 'Concordance table of days:' print date print 'List of files processed:' #Build a list of same day filenames, 5 max for a profile meter,skip first and last days for f in sorted(date.keys())[2:-1]: logs=[] for name in files: ext=name.split('.')[1] if ext=='txt': dat=name.split('_')[1].split('y')[1] if dat==f: logs.append(os.path.join(root,name)) #Open the files and read line by line datsec=False lines=[[] for i in range(5)] fn=0 for line in fileinput.input(logs): if line.split()[0]=='DataID': datsec=True ln=0 if datsec: lines[fn].append(line.split()) ln+=1 if ln==10255: datsec=False fileinput.nextfile() fn+=1 print fileinput.filename().rsplit('\\',1)[1] fileinput.close() aut='000_AutoLog'+f+'.log' out=os.path.join(root,aut) alf=open(out,'w') alf.write('Timestamp (mm/dd/ hh:mm:ss) VF 1VF 2VF 3 VF 4VF 5Q 1 Q 2 Q 3 Q 4 Q 5 Status State Metric Band Temperature 1 Band Temperature 2 Band Temperature 3 Band Temperature 4 Band Temperature 5 SPL 1 SPL 2 SPL 3 SPL 4 SPL 5'+'\n') for wn in range(1,10255,1): for i in range(5): lines[i][wn][2]=str(float(lines[i][wn][2])/fps) tp=lines[0][wn][0]+' '+lines[0][wn][1] vf=tab.join([lines[i][wn][2] for i in range(5)]) vq=tab.join([lines[i][wn][3] for i in range(5)]) vs=tab.join([lines[i][wn][4] for i in range(5)]) #sf=tab.join([lines[i][wn][5] for i in range(5)]) #sq=tab.join([lines[i][wn][6] for i in range(5)]) #ss=tab.join([lines[i][wn][7] for i in range(5)]) alf.write(tp+'\t'+vf+'\t'+vq+'\t'+al+'\t'+bt+'\t'+vs+'\n') alf.close() print "Done" -- http://mail.python.org/mailman/listinfo/python-list
Re: EOFError with fileinput
Here is an excerpt. It works because the end condition is a fixed number (ln==10255), the approximate number of data lines in a file. If I replace that condition by EOFError, the program does not do the intended work. It appears as if EOFError is always true in this case. +++ for line in fileinput.input(logs): if line.split()[0]=='DataID': datsec=True ln=0 if datsec: lines[fn].append(line.split()) ln+=1 if ln==10255: datsec=False fileinput.nextfile() fn+=1 print fileinput.filename().rsplit('\\',1)[1] fileinput.close() +++++++ Regards, Alex van der Spek "Alex van der Spek" wrote in message news:4c696751$0$22940$e4fe5...@news.xs4all.nl... Using the fileinput module to process lists of files: for line in fileinput.input(logs): Unfortunately, EOFError does not seem to indicate the end-of-file condition correctly when using fileinput. How would you find the EOF file for all the files in the file list 'logs'? Thank you, Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list
Re: EOFError with fileinput
Thanks all! I understand better now. I had no idea that EOFError was an exception. I was looking for a function to tell me when the end of a sequential file is reached as in all of the 4 programming languages that I do know this is a requirement. Will modify my program accordingly. Alex van der Spek -- http://mail.python.org/mailman/listinfo/python-list