Re: [Tutor] creating a tab delimited filename
C Smith wrote: Here's an example that cycles through 3 number, 0, 1, and 2: ### >>> j=0 >>> for i in range(10): .. print j .. j = (j+1)%3 .. 0 1 2 0 1 2 0 1 2 0 ### A nice way to do this is with itertools.cycle(): >>> import itertools >>> cyc = itertools.cycle(range(3)) >>> for i in range(10): ... print cyc.next() ... 0 1 2 0 1 2 0 1 2 0 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
Hi Jacob, Watch out with your code, ### if i==1000: i=0 else: i=i+1 ### Since this test is done after you have stored your data you are actually going to store 1001 pieces of data. i starts at 0 you store 0 you do your test and i is incremented {that's set 1} i is 1 you store 1 you do your test and i is incremented {that's set 2} ... i is 1000 you store 1000 you do your test and i is set to 0 {that's set 10001} Also, it looks like you have that i-incrementing happening at the same level as the if-test. If so, it is possible that the i's that are used to store the data will not increase by 1 each time since they are being incremented for every k and not only when the data is being stored. If you only want it incremented when something is stored, put it inside the if-block. Also, consider using the modulo operator to keep track of i: replace your test with ### i = (i+1)%1000 ### i starts and 0 and will be incremented until it is 999 and then when this calculation is then performed you will calculate (999+1)%1000 which is 1000%1000 which is 0--just what you wanted. Here's an example that cycles through 3 number, 0, 1, and 2: ### >>> j=0 >>> for i in range(10): .. print j .. j = (j+1)%3 .. 0 1 2 0 1 2 0 1 2 0 ### /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: Fwd: [Tutor] creating a tab delimited filename
Max Noel wrote: Forwarding to the list -- please use Reply to All. Begin forwarded message: From: jrlen balane <[EMAIL PROTECTED]> Date: March 16, 2005 04:13:40 GMT To: Max Noel <[EMAIL PROTECTED]> Subject: Re: [Tutor] creating a tab delimited filename Reply-To: jrlen balane <[EMAIL PROTECTED]> why is this not working, what i am trying to do is create a table like file based on 6 arrays. i 've used the forward slashes so i assume that the text file was created, but to no avail, there were no text file.: for k in range (rx_len-9): if byte[k] == 70 and byte [k+2] == 6 and sum(byte[k:k+10]) & 0xff == 0: temp1.append(byte[k+3]) temp2.append(byte[k+4]) pyra1.append(byte[k+5]) pyra2.append(byte[k+6]) voltage.append(byte[k+7]) current.append(byte[k+8]) print temp1[i], temp2[i], pyra1[i], pyra2[i], voltage[i], current[i] if i==1000: i = 0 else: i = i+1 ser.flushInput() filename = "%s%s.txt" %('C:/Documents and Settings/nyer/My Documents/Info',time.strftime("%Y%m%d%H%M")) print filename might be helpful here table_file = open(filename,"a") lendata = len(temp1) for ii in xrange(lendata): table_file.write('%i\t'%temp1[ii]) table_file.write('%i\t'%temp2[ii]) table_file.write('%i\t'%pyra1[ii]) table_file.write('%i\t'%pyra2[ii]) table_file.write('%i\t'%voltage[ii]) table_file.write('%i\t'%current[ii]) table_file.write('\n') table_file.close() might help...do you get any error messages or just no file written? Kent === it is printing the array so i assume that that the problem is in this table_file... please, i need help ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Fwd: [Tutor] creating a tab delimited filename
Forwarding to the list -- please use Reply to All. Begin forwarded message: From: jrlen balane <[EMAIL PROTECTED]> Date: March 16, 2005 04:13:40 GMT To: Max Noel <[EMAIL PROTECTED]> Subject: Re: [Tutor] creating a tab delimited filename Reply-To: jrlen balane <[EMAIL PROTECTED]> why is this not working, what i am trying to do is create a table like file based on 6 arrays. i 've used the forward slashes so i assume that the text file was created, but to no avail, there were no text file.: for k in range (rx_len-9): if byte[k] == 70 and byte [k+2] == 6 and sum(byte[k:k+10]) & 0xff == 0: temp1.append(byte[k+3]) temp2.append(byte[k+4]) pyra1.append(byte[k+5]) pyra2.append(byte[k+6]) voltage.append(byte[k+7]) current.append(byte[k+8]) print temp1[i], temp2[i], pyra1[i], pyra2[i], voltage[i], current[i] if i==1000: i = 0 else: i = i+1 ser.flushInput() filename = "%s%s.txt" %('C:/Documents and Settings/nyer/My Documents/Info',time.strftime("%Y%m%d%H%M")) table_file = open(filename,"a") lendata = len(temp1) for ii in xrange(lendata): table_file.write('%i\t'%temp1[ii]) table_file.write('%i\t'%temp2[ii]) table_file.write('%i\t'%pyra1[ii]) table_file.write('%i\t'%pyra2[ii]) table_file.write('%i\t'%voltage[ii]) table_file.write('%i\t'%current[ii]) table_file.write('\n') === it is printing the array so i assume that that the problem is in this table_file... please, i need help -- maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
On Mar 16, 2005, at 03:54, jrlen balane wrote: how would i make this the correct path: filename = "%s%s.txt" %('C:\Documents and Settings\nyer\My Documents\Info',time.strftime("%Y%m%d%H%M")) table_file = open(os.path.normpath(filename),"a") running on IDLE, i get the following error: Traceback (most recent call last): File "C:/Python23/practices/serialnewesttes3.py", line 65, in -toplevel- table_file = open(os.path.normpath(filename),"a") IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\nyer\\My Documents\\Info200503161153.txt' it seems that it adds "\". The "\n" of "\nyer" is escaped as a new line. That's a Bad Thing. In fact, relying on backslashes being automatically escaped thanks to the following character not having a special meaning in escape context is a Bad Thing. Either way, it's better to use forward slashes (or os.sep if you're going for overkill and/or obscure, non-POSIX platforms), with which such problems just don't happen. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
how would i make this the correct path: filename = "%s%s.txt" %('C:\Documents and Settings\nyer\My Documents\Info',time.strftime("%Y%m%d%H%M")) table_file = open(os.path.normpath(filename),"a") running on IDLE, i get the following error: Traceback (most recent call last): File "C:/Python23/practices/serialnewesttes3.py", line 65, in -toplevel- table_file = open(os.path.normpath(filename),"a") IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\nyer\\My Documents\\Info200503161153.txt' it seems that it adds "\". On Tue, 15 Mar 2005 19:45:31 -0500, Jacob S. <[EMAIL PROTECTED]> wrote: > > > > that is exactly what i am looking for, but how would i add this to my > > filename??? > > should i use this: > > > > output_file = open(os.path.join(self.Save_Session.GetValue(), > > time.strftime('%Y%m%d%H%M')), 'w') > > > > the self.Save_Session.GetValue() is generated by a dirdialog > > > > Uck,uck, uck, no... > > That's what Kent's post was for... > This will do the trick. > > filename = "%s%s.txt" % > (self.Save_Session.GetValue(),time.strftime("%Y%m%d%H%M")) > output_file = open(filename,"a") > > This uses string formatting, are you familiar with that? > > If not -- see here http://docs.python.org/lib/typesseq-strings.html#l2h-211 > remember, if you don't understand--not that you wouldn't--you can always ask > here. > > HTH, > Jacob > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
that is exactly what i am looking for, but how would i add this to my filename??? should i use this: output_file = open(os.path.join(self.Save_Session.GetValue(), time.strftime('%Y%m%d%H%M')), 'w') the self.Save_Session.GetValue() is generated by a dirdialog Uck,uck, uck, no... That's what Kent's post was for... This will do the trick. filename = "%s%s.txt" % (self.Save_Session.GetValue(),time.strftime("%Y%m%d%H%M")) output_file = open(filename,"a") This uses string formatting, are you familiar with that? If not -- see here http://docs.python.org/lib/typesseq-strings.html#l2h-211 remember, if you don't understand--not that you wouldn't--you can always ask here. HTH, Jacob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
that is exactly what i am looking for, but how would i add this to my filename??? should i use this: output_file = open(os.path.join(self.Save_Session.GetValue(), time.strftime('%Y%m%d%H%M')), 'w') the self.Save_Session.GetValue() is generated by a dirdialog On Tue, 15 Mar 2005 09:41:35 -0600, C Smith <[EMAIL PROTECTED]> wrote: > > On Tuesday, Mar 15, 2005, at 05:01 America/Chicago, > [EMAIL PROTECTED] wrote: > > > how am i going to change the filename automaticaly? > > for example: > > #every 5 minutes, i am going to create a file based on the > > data above > > for i in range(100) > > output_file = file('c:/output' +.join(i) +'.txt', 'w') > > #guess this won't work > > output_file.writelines(lines) > > output_file.close() > > > > When faced with this problem, one of the tings that I liked to do was > use a date stamp for the files. The time module has a function called > strftime which allows you to use a template to create a string from the > current time. (See the documentation of python module 'time' for more > details.) e.g. > > ### > >>> import time > >>> time.strftime('%Y%m%d%H%M') > '200503150934' > ### > > This string can be added to your 'c:/output' rather than a generic > number like i. Just to be safe you might want to check that the file > doesn't already exist. The nice thing is that there should be few files > that have this number added to them so the loop to get a valid name is > going to succeed quickly. > > /c > > > ___ > 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] creating a tab delimited filename
On Tuesday, Mar 15, 2005, at 05:01 America/Chicago, [EMAIL PROTECTED] wrote: how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() When faced with this problem, one of the tings that I liked to do was use a date stamp for the files. The time module has a function called strftime which allows you to use a template to create a string from the current time. (See the documentation of python module 'time' for more details.) e.g. ### >>> import time >>> time.strftime('%Y%m%d%H%M') '200503150934' ### This string can be added to your 'c:/output' rather than a generic number like i. Just to be safe you might want to check that the file doesn't already exist. The nice thing is that there should be few files that have this number added to them so the loop to get a valid name is going to succeed quickly. /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
jrlen balane wrote: so for example, i have 5 arrays, i can do this (is this correct): data1[1,2,3,4,5 (and so on)] data2[1,2,3,4,5 (and so on)] data3[1,2,3,4,5 (and so on)] data4[1,2,3,4,5 (and so on)] data5[1,2,3,4,5 (and so on)] datas = [data1, data2, data3, data4, data5] for data in datas: lines.append('\t'.join(data) + '\n') = (supposed to be output) 1 234 5 ... 1 234 5 ... 1 234 5 ... ... (but i want this output) 111 11 222 22 333 33 ... ... ... ...... = You need to reformat your data. zip() is handy for this. Given multiple lists as arguments, zip() creates tuples from the first element of each list, the second element of each list, etc: >>> a=[1,2,3] >>> b=[11,22,33] >>> zip(a, b) [(1, 11), (2, 22), (3, 33)] When all the source lists are themselves in a list, you can use the *args form of argument passing: >>> c=[a, b] >>> c [[1, 2, 3], [11, 22, 33]] >>> zip(*c) [(1, 11), (2, 22), (3, 33)] So in your case this will do what you want: for data in zip(*datas): lines.append('\t'.join(data) + '\n') If datas is large you might want to use itertools.izip() instead. zip() creates an intermediate list while izip() creates an iterator over the new tuples, so it is more memory-efficient. output_file = file('c:/output.txt', 'w') output_file.writelines(lines) output_file.close() = how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() Two ways to do this: >>> for i in range(5): ... print 'c:/output' + str(i) + '.txt' ... print 'c:/output%d.txt' % i ... c:/output0.txt c:/output0.txt c:/output1.txt c:/output1.txt ... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
On Mar 15, 2005, at 03:35, jrlen balane wrote: how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() I'm not going to give you a complete solution because that'd spoil the fun, but you need to use the os.path.exists function (in the os module), which tells you if a file exists or not (no need to open it -- actually, your loop is dangerous: opening a file with "w" as the mode erases it). As for generating the file name, you've almost got it. Just remove that bizarre call to "join", cause that won't work, and replace your for loop with a while loop, so that you can break out of it when you've found the correct file name, not before or after. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
so for example, i have 5 arrays, i can do this (is this correct): data1[1,2,3,4,5 (and so on)] data2[1,2,3,4,5 (and so on)] data3[1,2,3,4,5 (and so on)] data4[1,2,3,4,5 (and so on)] data5[1,2,3,4,5 (and so on)] datas = [data1, data2, data3, data4, data5] for data in datas: lines.append('\t'.join(data) + '\n') = (supposed to be output) 1 234 5 ... 1 234 5 ... 1 234 5 ... ... (but i want this output) 111 11 222 22 333 33 ... ... ... ...... = output_file = file('c:/output.txt', 'w') output_file.writelines(lines) output_file.close() = how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
On Sun, 13 Mar 2005, Brian van den Broek wrote: > jrlen balane said unto the world upon 2005-03-13 19:37: > > so for example, i am creating a text file with file.write() > > > > how am i going to make the file a tab-delimited file??? any > > parameters needed??? > > > > >>> record1 = ['Foo', 'Bar', 'Baz'] > >>> record2 = ['Ham', 'Spam', 'Eggs'] > >>> records = [record1, record2] > >>> lines = [] > >>> for record in records: > ... lines.append('\t'.join(record) + '\n') > ... The 'csv' module might also be really handy here. For example, we can adapt the code from: http://www.python.org/doc/lib/node617.html Let me write Brian's example with 'csv', just to show how it works. ## >>> record1 = ['Foo', 'Bar', 'Baz'] >>> record2 = ['Ham', 'Spam', 'Eggs'] >>> import sys >>> writer = csv.writer(sys.stdout) >>> for row in [record1, record2]: ... writer.writerow(row) ... Foo,Bar,Baz Ham,Spam,Eggs ## By default, the 'csv' writer uses commas as separators, but we can set the 'dialect' of a csv writer to the 'excel-tab' dialect, which uses tabs as delimiters: ## >>> writer = csv.writer(sys.stdout, dialect='excel-tab') >>> for row in [record1, record2]: ... writer.writerow(row) ... Foo Bar Baz Ham SpamEggs ## I hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
jrlen balane said unto the world upon 2005-03-13 19:37: so for example, i am creating a text file with file.write() how am i going to make the file a tab-delimited file??? any parameters needed??? >>> record1 = ['Foo', 'Bar', 'Baz'] >>> record2 = ['Ham', 'Spam', 'Eggs'] >>> records = [record1, record2] >>> lines = [] >>> for record in records: ... lines.append('\t'.join(record) + '\n') ... >>> lines ['Foo\tBar\tBaz\n', 'Ham\tSpam\tEggs\n'] >>> output_file = file('c:/output.txt', 'w') >>> output_file.writelines(lines) >>> output_file.close() >>> HTH, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
specifically, I want to write an array in to a text file, how am i going to make the file a tab-delimited file??? On Mon, 14 Mar 2005 08:37:35 +0800, jrlen balane <[EMAIL PROTECTED]> wrote: > so for example, i am creating a text file with > file.write() > > how am i going to make the file a tab-delimited file??? any parameters > needed??? > > > On Sun, 13 Mar 2005 15:59:46 -0800 (PST), Danny Yoo > <[EMAIL PROTECTED]> wrote: > > > > > > On Sun, 13 Mar 2005, jrlen balane wrote: > > > > > what does a tab delimited filename mean? how am i going to make this? > > > also how does it differs from space delimited, csv, and others? > > > > Hello, > > > > As Kent mentioned, you probably mean "tab delimited file", which means a > > file whose lines are split up into columns. Each column is separated by a > > tab, which, in theory, should make it easy to parse. > > > > The main difference between a tab-delimited file and the others you > > mention is the "delimiter", the separator that's chosen to break columns > > apart. > > > > By the way, you might be interested in: > > > > http://www.faqs.org/docs/artu/ch05s02.html#id2901882 > > > > which talks a lot more about file formats in Unix and their relative > > strengths and weaknesses. > > > > Best of wishes to you! > > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
so for example, i am creating a text file with file.write() how am i going to make the file a tab-delimited file??? any parameters needed??? On Sun, 13 Mar 2005 15:59:46 -0800 (PST), Danny Yoo <[EMAIL PROTECTED]> wrote: > > > On Sun, 13 Mar 2005, jrlen balane wrote: > > > what does a tab delimited filename mean? how am i going to make this? > > also how does it differs from space delimited, csv, and others? > > Hello, > > As Kent mentioned, you probably mean "tab delimited file", which means a > file whose lines are split up into columns. Each column is separated by a > tab, which, in theory, should make it easy to parse. > > The main difference between a tab-delimited file and the others you > mention is the "delimiter", the separator that's chosen to break columns > apart. > > By the way, you might be interested in: > > http://www.faqs.org/docs/artu/ch05s02.html#id2901882 > > which talks a lot more about file formats in Unix and their relative > strengths and weaknesses. > > Best of wishes to you! > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
On Sun, 13 Mar 2005, jrlen balane wrote: > what does a tab delimited filename mean? how am i going to make this? > also how does it differs from space delimited, csv, and others? Hello, As Kent mentioned, you probably mean "tab delimited file", which means a file whose lines are split up into columns. Each column is separated by a tab, which, in theory, should make it easy to parse. The main difference between a tab-delimited file and the others you mention is the "delimiter", the separator that's chosen to break columns apart. By the way, you might be interested in: http://www.faqs.org/docs/artu/ch05s02.html#id2901882 which talks a lot more about file formats in Unix and their relative strengths and weaknesses. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
jrlen balane wrote: what does a tab delimited filename mean? how am i going to make this? also how does it differs from space delimited, csv, and others? I think you probably mean "tab-delimited file", not "filename". A tab-delimited file is similar to a space-delimited file. It is typically a record-oriented text file with one record per line, with fields of a record separated by tabs. Like space-delimited files, this is a very easy format to read and write. Kent can't really find an article that could put me in the right direction so i posted here. thanks in advance. ___ 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] creating a tab delimited filename
what does a tab delimited filename mean? how am i going to make this? also how does it differs from space delimited, csv, and others? can't really find an article that could put me in the right direction so i posted here. thanks in advance. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor