Re: [Tutor] get columns from txt file

2012-07-14 Thread Dave Angel
On 07/13/2012 05:09 AM, susana moreno colomer wrote:
 
 
 Hi!
 I am sorry, but still I don't get it!

1) please don't top-post.  Put your new comments AFTER the part you're
quoting.  Or don't bother quoting, if you think your stuff stands alone.

2) please do a Reply-all, or equivalent.  Anyway, make sure the
python-tutor is included in the list of recipients.  Otherwise, you're
just sending private mail.


 I am trying with this (the code is attached)

3) Don't try to send attachments to the list, They don't work
everywhere.  This is a text list, so your messages should be plain text,
not html, and without colors, bold, or attachments.  For example, I
don't see any code.  But it doesn't matter, as more importantly, I don't
see the output of your code.

  
 csv_out=csv.writer(open('out1.csv', 'wb'), delimiter=' ', 
 quoting=csv.QUOTE_ALL, dialect='excel')
 and 
 csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel-tab')
  
 When I open out1 with text editor, I get 6 columns, but when I open it with 
 excel I get one column (6 numbers on each cell, how can I separate it it???)

As Marc requested in another message, please paste the first few lines
of that out1 text file into your reply, so somebody that knows Excel
(better than I) can try to guess why it's not being imported correctly.

  
 The files from wich I get the columns are txt files.
  
 Many thanks!!
 
SNIP
-- 

DaveA


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


Re: [Tutor] get columns from txt file

2012-07-13 Thread susana moreno colomer

Hi!
I am trying this, but still I get 6 numbers per cell. The only one difference 
is that I get a comma between numbers instead an space. 
I am opening the document also with excel
Many thanks, 
Susana
 



Subject: Re: [Tutor] get columns from txt file
From: dfjenni...@gmail.com
Date: Thu, 12 Jul 2012 12:14:38 -0400
CC: tutor@python.org
To: susana...@hotmail.com




On Jul 12, 2012, at 12:06 PM, susana moreno colomer wrote:



Hi!
This code is working fine!
The only one little thing is that the 6 columns appear together in one column, 
what means, in eac cell I get 6 numbers. How can I get tit in 6 excel columns?


Programming is hard, so don't feel bad that I'm having to tell you again to 
specify the dialect as excel or excel-tab:


csv_out=csv.writer(open('out14.csv', 'wb'), dialect='excel')


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


Re: [Tutor] get columns from txt file

2012-07-13 Thread Marc Tompkins
On Thu, Jul 12, 2012 at 11:36 PM, susana moreno colomer 
susana...@hotmail.com wrote:

  Hi!
 I am trying this, but still I get 6 numbers per cell. The only one
 difference is that I get a comma between numbers instead an space.
 I am opening the document also with excel
 Many thanks,
 Susana


CSV stands for Comma Separated Values.  Those commas are the separators
between cells.  Your current problem is that a CSV file is just a regular
text file (that happens to have a lot of commas in it), and Excel is trying
to read it as a normal text file.

CSV is about the simplest way ever invented to store tabular data in a
file, but there are some complicating factors.  The most obvious is: what
happens if the data you want to store in your file actually contains commas
(e.g. address fields with city, state zip, or numbers with thousands
separators, etc.)  One way around the problem is to put quotes around
fields, and then separate the fields with commas (but then, what if your
data contains quotes?); another is to separate the fields with tab
characters instead of commas (technically this isn't really a CSV file
anymore, but the acronym TSV never caught on.)

Excel's native flavor* of CSV is the oldest, simplest, and stupidest of all
- just commas between fields, and newlines between records:
1, 2, 3, 4, 5
a, b, c, d, e

Quotes-and-commas style:
1, 2, 3, 4,000,000, 5
a, b, c, Dammit, Janet, e

Tab-separated (well, you'll just have to imagine; I don't feel like
reconfiguring my text editor):
1   2   3  4  5
a  b  cde fghij

and a bunch of others I can't think of right now.
* Note: Excel will happily import a quotes-and-commas CSV file and display
it normally - but if you export it to CSV, it will revert to the dumb
bare-commas format.

From the Python csv module docs:

 To make it easier to specify the format of input and output records,
 specific formatting parameters are grouped together into dialects. A
 dialect is a subclass of the 
 Dialecthttp://docs.python.org/library/csv.html#csv.Dialectclass having a 
 set of specific methods and a single
 validate() method.


So you can specify which dialect you want to read or write, and/or you can
specify which delimiter(s) you want to use.

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


Re: [Tutor] get columns from txt file

2012-07-13 Thread Marc Tompkins
Reply to the group, please!

On Fri, Jul 13, 2012 at 2:09 AM, susana moreno colomer 
susana...@hotmail.com wrote:


 Hi!
 I am sorry, but still I don't get it!
 I am trying with this (the code is attached)

 csv_out=csv.writer(open('out1.csv', 'wb'), delimiter=' ',
 quoting=csv.QUOTE_ALL, dialect='excel')
 and
 csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel-tab')

 When I open out1 with text editor, I get 6 columns, but when I open it
 with excel I get one column (6 numbers on each cell, how can I separate
 it it???)

 The files from wich I get the columns are txt files.

 Many thanks!!


It sounds more like an Excel import problem than a Python output problem at
this point, but it's hard to tell.

Is the data you're dealing with private/confidential?  If not, could you
attach your output CSV file?  A quick look could take the place of hours of
guesswork.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-13 Thread Joel Goldstick
On Fri, Jul 13, 2012 at 5:21 AM, Marc Tompkins marc.tompk...@gmail.com wrote:
 Reply to the group, please!

 On Fri, Jul 13, 2012 at 2:09 AM, susana moreno colomer
 susana...@hotmail.com wrote:


 Hi!
 I am sorry, but still I don't get it!
 I am trying with this (the code is attached)

 csv_out=csv.writer(open('out1.csv', 'wb'), delimiter=' ',
 quoting=csv.QUOTE_ALL, dialect='excel')
 and
 csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel-tab')

You need to refer to the spec:  csv.writer(csvfile[,
dialect='excel'][, fmtparam])
(http://docs.python.org/library/csv.html#csv.writer)

so i would use:
csv_out = csv.writer(open('out1.csv', 'wb'), dialect='excel')

This will give you output separated by commas and (if I am not
mistaken) is will only quote values that are strings.


 When I open out1 with text editor, I get 6 columns, but when I open it
 with excel I get one column (6 numbers on each cell, how can I separate it
 it???)

 The files from wich I get the columns are txt files.

 Many thanks!!


 It sounds more like an Excel import problem than a Python output problem at
 this point, but it's hard to tell.

 Is the data you're dealing with private/confidential?  If not, could you
 attach your output CSV file?  A quick look could take the place of hours of
 guesswork.



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

I agree with Marc's comments.  I would use the comma separated value
version of the output file, not the tabbed version.

So if your file looks like this in a text editor:

1,2,3,4,5,6

it will put each value in successive columns.


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


Re: [Tutor] get columns from txt file

2012-07-13 Thread susana moreno colomer


Hi!
I am sorry, but still I don't get it!
I am trying with this (the code is attached)
 
csv_out=csv.writer(open('out1.csv', 'wb'), delimiter=' ', 
quoting=csv.QUOTE_ALL, dialect='excel')
and 
csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel-tab')
 
When I open out1 with text editor, I get 6 columns, but when I open it with 
excel I get one column (6 numbers on each cell, how can I separate it it???)
 
The files from wich I get the columns are txt files.
 
Many thanks!!



Date: Fri, 13 Jul 2012 00:28:07 -0700
Subject: Re: [Tutor] get columns from txt file
From: marc.tompk...@gmail.com
To: susana...@hotmail.com
CC: dfjenni...@gmail.com; tutor@python.org


On Thu, Jul 12, 2012 at 11:36 PM, susana moreno colomer susana...@hotmail.com 
wrote:



Hi!
I am trying this, but still I get 6 numbers per cell. The only one difference 
is that I get a comma between numbers instead an space. 
I am opening the document also with excel
Many thanks, 
Susana


CSV stands for Comma Separated Values.  Those commas are the separators between 
cells.  Your current problem is that a CSV file is just a regular text file 
(that happens to have a lot of commas in it), and Excel is trying to read it as 
a normal text file. 

CSV is about the simplest way ever invented to store tabular data in a file, 
but there are some complicating factors.  The most obvious is: what happens if 
the data you want to store in your file actually contains commas (e.g. address 
fields with city, state zip, or numbers with thousands separators, etc.)  One 
way around the problem is to put quotes around fields, and then separate the 
fields with commas (but then, what if your data contains quotes?); another is 
to separate the fields with tab characters instead of commas (technically this 
isn't really a CSV file anymore, but the acronym TSV never caught on.)  

Excel's native flavor* of CSV is the oldest, simplest, and stupidest of all - 
just commas between fields, and newlines between records:
1, 2, 3, 4, 5
a, b, c, d, e

Quotes-and-commas style:
1, 2, 3, 4,000,000, 5
a, b, c, Dammit, Janet, e

Tab-separated (well, you'll just have to imagine; I don't feel like 
reconfiguring my text editor):
1   2   3  4  5
a  b  cde fghij

and a bunch of others I can't think of right now.
* Note: Excel will happily import a quotes-and-commas CSV file and display it 
normally - but if you export it to CSV, it will revert to the dumb bare-commas 
format.

From the Python csv module docs:

To make it easier to specify the format of input and output records, specific 
formatting parameters are grouped together into dialects. A dialect is a 
subclass of the Dialect class having a set of specific methods and a single 
validate() method. 


So you can specify which dialect you want to read or write, and/or you can 
specify which delimiter(s) you want to use.

Hope that helps... 


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


Re: [Tutor] get columns from txt file

2012-07-13 Thread Prasad, Ramit
Please always reply to the list!

 
 Hi!
 I am sorry, but still I don't get it!
 I am trying with this (the code is attached)
 
 csv_out=csv.writer(open('out1.csv', 'wb'), delimiter=' ',
 quoting=csv.QUOTE_ALL, dialect='excel')
 and
 csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel-tab')
 
 When I open out1 with text editor, I get 6 columns, but when I open it with
 excel I get one column (6 numbers on each cell, how can I separate it it???)
 
 The files from wich I get the columns are txt files.
 
 Many thanks!!

Use
csv_out=csv.writer(open('out1.csv', 'wb'), dialect='excel')
NOT dialect 'excel-tab' or with the delimiter field.

Alternatively try following these steps,
http://www.ehow.com/how_4449036_import-delimited-file-excel.html



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread Don Jennings
(Please use reply all so the message gets posted for all to see :)

On Jul 12, 2012, at 7:14 AM, susana moreno colomer wrote:

 Hi!
 Many thanks for your help.
 Now I am trying this, but I get a blank excel document

Not surprising as there are several errors in your code. In fact, I'm surprised 
it ran without reporting an error. Be sure to copy and paste the code exactly 
as you are running it.

 (I have tried everything, but I don't get it!)

That's why we're here :)

As I understand it, you have a file structure like this:

dir/
bb_csvfile1.txt
bb_csvfile2.txt
bb_csvfilen.txt
someotherfile.txt

You want to read each of the csv files which start with 'bb_', extracting the 
data in a certain column, and then output that data to a single excel document. 
Yes?


  
 import os
 import fnmatch
 import csv
 
 path = '...'

First of all, this path variable looks odd. Do you mean '.' for the current 
directory or '..' for the parent directory?

 csv_out=csv.writer(open('out11.csv', 'wb'), delimiter='  ')

Since you said you want an excel document, you should specify dialect='excel'  
(or 'excel-tab') instead of a delimiter.

 files=os.listdir(path)
  
 for infile in files:
  output=[] 

The variable output should be moved outside the for loop (i.e. put it before 
the for infile in files:)

  if fnmatch.fnmatch(infile, 'bb_*'):
   global filename

Drop the global statement. Out of curiousity, why did you think you needed 
that? (There was no use of global in the example code from Kent Johnson.)

   filename= path+infile

Won't work (even if the path variable was correct)—where's the file path 
separator? Use the join function of os.path:

filename = os.path.join(path, infile)

 
   global f

Again, no need for the global statement.

   f=open(filename, 'r')

   for line in f:
  
b=line.split('\t')
output.append(b[5].strip())
   
def csvwriter():

No need for a function here. In fact, you don't ever actually call it, so it 
never gets run! That's a major reason why you aren't getting any output.

 csv_out.append(output)
 csv_out.append('\n')

These should be outside of the for loops as well (i.e. remove all indentation).

   f.close


You need the parentheses to call the function and it should be at the same 
level as the call where you open it (i.e. the same amount of indentation, 
before the calls to csv_out.append):

f.close()

Take care,
Don

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


Re: [Tutor] get columns from txt file

2012-07-12 Thread Don Jennings
Oops! Still you forgot to cc: the tutor list. It's really important because if 
someone (like me, for instance) steers you in the wrong direction, others will 
jump in with corrections.


On Jul 12, 2012, at 9:48 AM, susana moreno colomer wrote:

 Hi!
 Many thanks!

You're welcome. I see that your code is improving :)

 Still I get an error: AttributeError: '_cvs.writer' object has no atribute 
 'append'.

If you would like a response to that error, please send the exact code you 
tried which didn't work and the error message by copying/pasting. (See, I'm 
pretty sure that you typed in the error above since you misspelled attribute as 
'atribute'.)

 I wanted to solve it with the Def function.

And that's why you should send the original code and error. Now, you have a 
different problem, but it's a good time to clear up your confusion. Def is not 
a function. In fact, I don't know what Def is. Instead, the def 
statement—notice that it's all lower case—defines a function which you can call 
elsewhere in code.

 def call_me():
... print inside the call_me function
... 
 call_me()
inside the call_me function

Note, that there is no output from our defining the call_me function; it's only 
when we call it with the parentheses that the code inside of it is executed. 
Make sense? However, again, it's not necessary for this script. Once you have 
the csv_out variable, you should be able to 

 Now I am trying this, though I get
  
  
 import os
 import fnmatch
 import csv
 
 path = '/This is my current directory/'
 csv_out=csv.writer(open('out13.csv', 'wb'), delimiter=' ')
 files=os.listdir(path)
 outfile=open('myfile1', 'w')
 output=[]
 
 for infile in files:
   if fnmatch.fnmatch(infile, 'bb_*'):
   filename= os.path.join(path,infile)
   f=open(filename, 'r')
   
   for line in f:
   b=line.split('\t')
   output.append(b[5].strip())
  f.close()
  
 Def csvwriter():   gives me SyntaxError: invalid syntax
 excelfile=csv_out
 a=excelfile.append(output)
 c=excelfile.write(a)
 return c
  
  
 I am trying with this  because the atribute 'writerows' didn't work.

A different error. There are lots of reasons it might not have worked. Wish you 
had sent us the code for that one :( We can't solve it without all the info.

 Is there another way to write in csv files?

Absolutely. At the interactive prompt, dir() shows all the attributes of an 
object to find out your options:

 csv_out=csv.writer(open('out13.csv', 'wb'), dialect='excel')
 dir(csv_out)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 
'writerow', 'writerows']

See the writerow and writerows? Those look promising. Alternatively, while it 
takes a while to learn to read the python documentation, it really is your 
friend:

http://docs.python.org/library/csv.html

If you'd like further examples, Doug Hellman often provides a great resource 
with his Python Module of the Week (PYMTOW) series:

http://www.doughellmann.com/PyMOTW/csv/

 I don't know what you mean with specify dialect

Something like this:

csv_out = csv.writer(fileobject, dialect='excel')

Take care,
Don

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


Re: [Tutor] get columns from txt file

2012-07-12 Thread susana moreno colomer

Hi!
 
I have a group of files in a directory:
bb_1.txt
bb_2.txt
bb_3.txt
bb_4.txt
bb_5.txt
bb_6.txt
ss_1.txt

I want to extract from  files whose names start with  bb_   column number 5 to 
an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th 
column from each file)
This code is working,with the following errors:


I get the data in only one column, instead of six
 
Many thanks!

 



Subject: Re: [Tutor] get columns from txt file
From: dfjenni...@gmail.com
Date: Thu, 12 Jul 2012 10:26:30 -0400
CC: tutor@python.org
To: susana...@hotmail.com

Oops! Still you forgot to cc: the tutor list. It's really important because if 
someone (like me, for instance) steers you in the wrong direction, others will 
jump in with corrections.





On Jul 12, 2012, at 9:48 AM, susana moreno colomer wrote:


Hi!
Many thanks!


You're welcome. I see that your code is improving :)




Still I get an error: AttributeError: '_cvs.writer' object has no atribute 
'append'. 

If you would like a response to that error, please send the exact code you 
tried which didn't work and the error message by copying/pasting. (See, I'm 
pretty sure that you typed in the error above since you misspelled attribute as 
'atribute'.)




I wanted to solve it with the Def function.

And that's why you should send the original code and error. Now, you have a 
different problem, but it's a good time to clear up your confusion. Def is not 
a function. In fact, I don't know what Def is. Instead, the def 
statement—notice that it's all lower case—defines a function which you can call 
elsewhere in code.



 def call_me():
... print inside the call_me function
... 
 call_me()
inside the call_me function


Note, that there is no output from our defining the call_me function; it's only 
when we call it with the parentheses that the code inside of it is executed. 
Make sense? However, again, it's not necessary for this script. Once you have 
the csv_out variable, you should be able to 




Now I am trying this, though I get
 
 
import os
import fnmatch
import csv

path = '/This is my current directory/'
csv_out=csv.writer(open('out13.csv', 'wb'), delimiter=' ')
files=os.listdir(path)
outfile=open('myfile1', 'w')
output=[]

for infile in files:
  if fnmatch.fnmatch(infile, 'bb_*'):
  filename= os.path.join(path,infile)
  f=open(filename, 'r')
  
  for line in f:
  b=line.split('\t')
  output.append(b[5].strip())
 f.close()
 
Def csvwriter():   gives me SyntaxError: invalid syntax
excelfile=csv_out
a=excelfile.append(output)
c=excelfile.write(a)
return c
 
 
I am trying with this  because the atribute 'writerows' didn't work.


A different error. There are lots of reasons it might not have worked. Wish you 
had sent us the code for that one :( We can't solve it without all the info.



Is there another way to write in csv files?


Absolutely. At the interactive prompt, dir() shows all the attributes of an 
object to find out your options:


 csv_out=csv.writer(open('out13.csv', 'wb'), dialect='excel')
 dir(csv_out)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 
'writerow', 'writerows']


See the writerow and writerows? Those look promising. Alternatively, while it 
takes a while to learn to read the python documentation, it really is your 
friend:


http://docs.python.org/library/csv.html


If you'd like further examples, Doug Hellman often provides a great resource 
with his Python Module of the Week (PYMTOW) series:


http://www.doughellmann.com/PyMOTW/csv/





I don't know what you mean with specify dialect


Something like this:


csv_out = csv.writer(fileobject, dialect='excel')


Take care,
Don
  #! /usr/bin/env python


import os
import fnmatch
import csv


path = '//../my_working_folder/'
csv_out=csv.writer(open('out14.csv', 'wb'), delimiter=' ')
files=os.listdir(path)

outfile=open('myfile1', 'w')
output=[]


for infile in files:

if fnmatch.fnmatch(infile, 'bb_*'):
filename= os.path.join(path,infile)
f=open(filename, 'r')

for line in f:

b=line.split('\t')
output.append(b[5].strip())
f.close()

This gives me a single column (I want 6, since I have 6 bb_files:

csv_out.writerows(output)


with this I get excel whitesheet


def csvwriter():
excelfile=csv_out
a=excelfile.append(output)
c=excelfile.write(a)
return c___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread Don Jennings

On Jul 12, 2012, at 10:55 AM, susana moreno colomer wrote:

 Hi!
  
 I have a group of files in a directory:
 bb_1.txt
 bb_2.txt
 bb_3.txt
 bb_4.txt
 bb_5.txt
 bb_6.txt
 ss_1.txt
 
 I want to extract from  files whose names start with  bb_   column number 5 
 to an excel file. I have 6  bb_  files, therefore I want to get 6 columns 
 (5th column from each file)
 This code is working,with the following errors:
 
 I get the data in only one column, instead of six

Great! It sounds like you're almost there. Where's the code which works except 
that it puts the data all in one column?

Take care,
Don___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread susana moreno colomer

Hi!
It is attached on the email, called myprogram.txt
Thank you!
 



Subject: Re: [Tutor] get columns from txt file
From: dfjenni...@gmail.com
Date: Thu, 12 Jul 2012 11:07:53 -0400
CC: tutor@python.org
To: susana...@hotmail.com




On Jul 12, 2012, at 10:55 AM, susana moreno colomer wrote:


Hi!
 
I have a group of files in a directory:
bb_1.txt
bb_2.txt
bb_3.txt
bb_4.txt
bb_5.txt
bb_6.txt
ss_1.txt

I want to extract from  files whose names start with  bb_   column number 5 to 
an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th 
column from each file)
This code is working,with the following errors:



I get the data in only one column, instead of six

Great! It sounds like you're almost there. Where's the code which works except 
that it puts the data all in one column?


Take care,
Don   #! /usr/bin/env python


import os
import fnmatch
import csv


path = '//../my_working_folder/'
csv_out=csv.writer(open('out14.csv', 'wb'), delimiter=' ')
files=os.listdir(path)

outfile=open('myfile1', 'w')
output=[]


for infile in files:

if fnmatch.fnmatch(infile, 'bb_*'):
filename= os.path.join(path,infile)
f=open(filename, 'r')

for line in f:

b=line.split('\t')
output.append(b[5].strip())
f.close()

This gives me a single column (I want 6, since I have 6 bb_files:

csv_out.writerows(output)


with this I get excel whitesheet


def csvwriter():
excelfile=csv_out
a=excelfile.append(output)
c=excelfile.write(a)
return c___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread Don Jennings

On Jul 12, 2012, at 11:10 AM, susana moreno colomer wrote:

 Hi!
 It is attached on the email, called myprogram.txt

and, here are the contents of that file:

 #! /usr/bin/env python
 
 
 import os
 import fnmatch
 import csv
 
 
 path = '//../my_working_folder/'
 csv_out=csv.writer(open('out14.csv', 'wb'), delimiter=' ')
 files=os.listdir(path)
 
 outfile=open('myfile1', 'w')

Here you've opened a file called outfile which you never use! So, of course 
it's blank. Just remove this line.

 output=[]
 
 
 for infile in files:
  columnData = []
   if fnmatch.fnmatch(infile, 'bb_*'):
   filename= os.path.join(path,infile)
   f=open(filename, 'r')
   
   for line in f:
   
   b=line.split('\t')
  # remove the next line
   output.append(b[5].strip())
  # instead, append to columnData list
  columnData.append(b[5].strip())
   f.close()

  # now append all of that data as a list to the output
  output.append(columnData)

  # now, as Kent said, create a list of row lists from the list of column lists
  # if any of the column lists are too short they will be padded with None

  rows = map(None, *output)

  # now, write those rows to your file
  csv_out.writerows(rows)

 
 This gives me a single column (I want 6, since I have 6 bb_files:
 
 csv_out.writerows(output)

One of the things you missed in Kent's code is that the output is a list of 
lists. So, for each file you need a list which you then append to the output 
list. I've inserted the appropriate code above and you should have better luck 
if you copy and paste carefully.

 
 
 with this I get excel whitesheet
 
 
 def csvwriter():  
   excelfile=csv_out
   a=excelfile.append(output)
   c=excelfile.write(a)
   return c

Right! As I explained in an earlier email, you never call the function 
csvwriter; you merely define it. The file is created when you opened it earlier 
for writing on line 10 of your program, but you never write anything to it.

If you have the time and inclination, I recommend you work through one of the 
fine tutorials for python. It really is a great language and this is a 
**great** community of folks for people like me who are learning the language 
still.

Take care,
Don



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


Re: [Tutor] get columns from txt file

2012-07-12 Thread susana moreno colomer


Hi!
This code is working fine!
The only one little thing is that the 6 columns appear together in one column, 
what means, in eac cell I get 6 numbers. How can I get tit in 6 excel columns?
Many thanks, 
Susana



Subject: Re: [Tutor] get columns from txt file
From: dfjenni...@gmail.com
Date: Thu, 12 Jul 2012 11:43:02 -0400
CC: tutor@python.org
To: susana...@hotmail.com




On Jul 12, 2012, at 11:10 AM, susana moreno colomer wrote:


Hi!
It is attached on the email, called myprogram.txt

and, here are the contents of that file:





#! /usr/bin/env python




import os
import fnmatch
import csv




path = '//../my_working_folder/'
csv_out=csv.writer(open('out14.csv', 'wb'), delimiter=' ')
files=os.listdir(path)


outfile=open('myfile1', 'w')

Here you've opened a file called outfile which you never use! So, of course 
it's blank. Just remove this line.



output=[]




for infile in files:  columnData = []


if fnmatch.fnmatch(infile, 'bb_*'):
filename= os.path.join(path,infile)
f=open(filename, 'r')

for line in f:

b=line.split('\t')  # remove the next line


output.append(b[5].strip())  # instead, append to 
columnData list
  columnData.append(b[5].strip())


f.close()

  # now append all of that data as a list to the output
  output.append(columnData)


  # now, as Kent said, create a list of row lists from the list of column lists
  # if any of the column lists are too short they will be padded with None


  rows = map(None, *output)


  # now, write those rows to your file
  csv_out.writerows(rows)






This gives me a single column (I want 6, since I have 6 bb_files:


csv_out.writerows(output)

One of the things you missed in Kent's code is that the output is a list of 
lists. So, for each file you need a list which you then append to the output 
list. I've inserted the appropriate code above and you should have better luck 
if you copy and paste carefully.








with this I get excel whitesheet




def csvwriter(): 
excelfile=csv_out
a=excelfile.append(output)
c=excelfile.write(a)
return c


Right! As I explained in an earlier email, you never call the function 
csvwriter; you merely define it. The file is created when you opened it earlier 
for writing on line 10 of your program, but you never write anything to it.


If you have the time and inclination, I recommend you work through one of the 
fine tutorials for python. It really is a great language and this is a 
**great** community of folks for people like me who are learning the language 
still.


Take care,
Don





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


Re: [Tutor] get columns from txt file

2012-07-12 Thread Don Jennings

On Jul 12, 2012, at 12:06 PM, susana moreno colomer wrote:

 
 Hi!
 This code is working fine!
 The only one little thing is that the 6 columns appear together in one 
 column, what means, in eac cell I get 6 numbers. How can I get tit in 6 excel 
 columns?

Programming is hard, so don't feel bad that I'm having to tell you again to 
specify the dialect as excel or excel-tab:

csv_out=csv.writer(open('out14.csv', 'wb'), dialect='excel')

Take care,
Don___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread Prasad, Ramit
 Hi!
 This code is working fine!
 The only one little thing is that the 6 columns appear together in one 
 column, what means, in eac cell I get 6 numbers. How can I get tit in 6 
 excel columns?

 Programming is hard, so don't feel bad that I'm having to tell you again to 
 specify the dialect as excel or excel-tab:

 csv_out=csv.writer(open('out14.csv', 'wb'), dialect='excel')

The default dialect is excel but it is important to also open
the file using Excel as it may default to a text editor.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-12 Thread Emile van Sebille

On 7/12/2012 9:06 AM susana moreno colomer said...


Hi!
This code is working fine!
The only one little thing is that the 6 columns appear together in one
column, what means, in eac cell I get 6 numbers. How can I get tit in 6
excel columns?



You're creating a list (output) with only one column in it, so you only 
get one column in excel.


Try making these chages (untested):


outfile=open('myfile1', 'w')
cols=[]   #  this will get one list (column) per found file

for infile in files:
if fnmatch.fnmatch(infile, 'bb_*'):
thiscol = []  # we're starting a new file so start a new column
filename= os.path.join(path,infile)
f=open(filename, 'r')
for line in f:
b=line.split('\t')
thiscol.append(b[5].strip())  # append each col value
f.close()
cols.append(thiscol)  # store this cols results

output = zip(cols) # zip transposes the cols to rows


HTH,

Emile


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


Re: [Tutor] get columns from txt file

2012-07-12 Thread Mark Lawrence

Sorry if this shows up twice

On 12/07/2012 17:18, Prasad, Ramit wrote:



csv_out=csv.writer(open('out14.csv', 'wb'), dialect='excel')


The default dialect is excel but it is important to also open
the file using Excel as it may default to a text editor.


In the context we're talking about here this makes no sense to me so can 
you please explain.





Ramit




--
Cheers.

Mark Lawrence.



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


Re: [Tutor] get columns from txt file

2012-07-12 Thread Prasad, Ramit
  csv_out=csv.writer(open('out14.csv', 'wb'), dialect='excel')
 
  The default dialect is excel but it is important to also open
  the file using Excel as it may default to a text editor.
 
 In the context we're talking about here this makes no sense to me so can
 you please explain.


Sorry, I think it was something that the OP said in a different email
that led me to believe that maybe she was not opening it in excel.
So I was pointing out that dialect defaults to excel to Don (although, 
explicitly setting it never hurt), and then making sure Susana
opened the resulting file in excel.

Not sure what I was thinking looking back...

Ramit

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] get columns from txt file

2012-07-11 Thread susana moreno colomer

Hi!
 
I have a group of files in a directory.
I want to extract from  files whose names start with  bb_   column number 5 to 
an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th 
column from each file)
This code is working,with the following errors:

I get the data in only one column, instead of six
I get white cell after every extracted cell
 
I've got  help from 
http://mail.python.org/pipermail/tutor/2004-November/033474.html, though it is 
not working for me
 
This is my code:
 
 
import os
import fnmatch
import csv

path = '//..'
files=os.listdir(path)
csv_out=csv.writer(open('out.csv', 'w'), delimiter='  ')

for infile in files:
   
if fnmatch.fnmatch(infile, 'bb_*'):
print infile
   
filename= path+infile
print filename

f=open(filename)
for line in f.readlines():
   b=line.split('\t')
   csv_out.writerow(b[5])
   f.close
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-11 Thread Don Jennings

On Jul 11, 2012, at 10:21 AM, tutor-requ...@python.org wrote:
 
 Message: 4
 Date: Wed, 11 Jul 2012 16:20:05 +0200
 From: susana moreno colomer susana...@hotmail.com
 To: tutor@python.org
 Subject: [Tutor] get columns from txt file
 Message-ID: bay164-w2886be3c3d5a8b0e3d496384...@phx.gbl
 Content-Type: text/plain; charset=iso-8859-1
 
 
 Hi!
 
 I have a group of files in a directory.
 I want to extract from  files whose names start with  bb_   column number 5 
 to an excel file. I have 6  bb_  files, therefore I want to get 6 columns 
 (5th column from each file)
 This code is working,with the following errors:
 
 I get the data in only one column, instead of six
 I get white cell after every extracted cell
 
 I've got  help from 
 http://mail.python.org/pipermail/tutor/2004-November/033474.html, though it 
 is not working for me


If I understand your requirements correctly, you should read the followup 
message in that thread:

http://mail.python.org/pipermail/tutor/2004-November/033475.html

Primarily, you'll just have to alter one of the code samples to write it to csv.

Take care,
Don

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


Re: [Tutor] get columns from txt file

2012-07-11 Thread Prasad, Ramit
 I have a group of files in a directory.
 I want to extract from  files whose names start with  bb_   column number 5 to
 an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th
 column from each file)
 This code is working,with the following errors:
 * I get the data in only one column, instead of six
 * I get white cell after every extracted cell
 
 I've got  help from http://mail.python.org/pipermail/tutor/2004-
 November/033474.html, though it is not working for me
 
 This is my code:
 
 
 import os
 import fnmatch
 import csv
 
 path = '//..'
 files=os.listdir(path)
 csv_out=csv.writer(open('out.csv', 'w'), delimiter='  ')
 
 for infile in files:
 
 if fnmatch.fnmatch(infile, 'bb_*'):
 print infile
 
 filename= path+infile
 print filename
 
 f=open(filename)
 for line in f.readlines():
b=line.split('\t')
csv_out.writerow(b[5])
f.close

You get 6 lines because csv_out.writerow writes a row and then
goes to the next line.

What you want to do is read through each file and append each
value to a list. Once you are done reading, write the list using
the csv module.

I am not sure what you mean by white cell after every extracted
cell but if you mean spaces you can use (where output is a list).

output.append(b[5].strip()) 

Also, csv files should be opened using 'wb' not 'w' otherwise
you will get extra lines in the file. Maybe this is what you
meant by white cell?


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor