[Tutor] Help merge files using python

jarod_v6 at libero.it jarod_v6 at libero.it
Wed Nov 13 23:26:51 CET 2013


Hi I want to merge many files like this:
#file1
A	10
B	20
C	30
#file2
B	45
Z	10
#file1
A	60
B	70
C	10

I want to obtain

A 10 0 60
B 20 45 70
C 30 0 10
Z 0 10 0

I try to do like this:
 f = os.listdir(".")
for i in f:
 T =open(i,"r")
for r in t.readlines():
print r

Could you please help me to understand how to use the right procedure in 
python?Thanks a lot!
bw


----Messaggio originale----
>Da: tutor-request at python.org
>Data: 23/10/2013 1.43
>A: <tutor at python.org>
>Ogg: Tutor Digest, Vol 116, Issue 58
>
>Send Tutor mailing list submissions to
>	tutor at python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	https://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>	tutor-request at python.org
>
>You can reach the person managing the list at
>	tutor-owner at python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. Re: Beginner Question (Andy McKenzie)
>   2. Re: Beginner Question (Dave Angel)
>   3. comma in an assignment (Key, Gregory E (E S SF RNA FSF 1 C))
>   4. Howto handle pictures with pyqt (Ulrich Goebel)
>   5. Re: Beginner Question (Sven Hennig)
>
>
>----------------------------------------------------------------------
>
>Message: 1
>Date: Tue, 22 Oct 2013 13:26:08 -0400
>From: Andy McKenzie <amckenzie4 at gmail.com>
>To: "tutor at python.org" <tutor at python.org>
>Subject: Re: [Tutor] Beginner Question
>Message-ID:
>	<CAJttDkrEpao=0FZecbP4ntbNu3A2zZ1fthhT=JroG8qRj97ifw at mail.gmail.com>
>Content-Type: text/plain; charset="iso-8859-1"
>
>On Tue, Oct 22, 2013 at 10:25 AM, Sven Hennig <shennig93 at googlemail.
com>wrote:
>
>> Hello, I would like to learn a programming language and have decided to
>> use Python. I have some programming experience and doing well in Python.
>> What really causes me problems is OOP.
>> I'm just dont get it... I'm missing a really Practical example. In every
>> book I've read are the examples of such Class Dog and the function is bark
>> . Has anyone an OOP example for me as it is really used in real code, so
>> I can better understand the concept? I do not know why this is so hard for
>> me.
>>
>> Greetings
>> Sven
>>
>>
>The actual code I have is in PHP, so it's not exactly useful to post here,
>but I can give you an outline of a class I used and found useful.  The
>class was created to help me manipulate network subnets in an interface to
>handle DHCP and DNS on a server.
>
>class subnet:
>
>   def __init__(cidr):
>      # Do some stuff with the CIDR notation of the subnet (1.2.3.0/24syntax)
>
>  def netmask:
>     # Calculate the netmask and return it (ie, 255.255.255.0)
>
>   def first_ip:
>      # Calculate and return the first IP in the range.
>
>   def last_ip:
>      # Calculate and return the last IP in the range
>
>   def number_of_addresses:
>      # Calculate the number of usable addresses in the range and return
>that value
>
>The benefit to me was that I could create an instance of the subnet object
>for a group (say, "lab_1"), and then pull out various information.  Calling
>"lab_1.first_ip()" returns the first possible IP address.  That was a lot
>more readable and a lot more concise than something like "first_ip(
>1.2.3.0/24)".
>
>
>I hope this helps!
>
>Andy
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.
org/pipermail/tutor/attachments/20131022/f6dd19dd/attachment-0001.html>
>
>------------------------------
>
>Message: 2
>Date: Tue, 22 Oct 2013 17:55:29 +0000 (UTC)
>From: Dave Angel <davea at davea.name>
>To: tutor at python.org
>Subject: Re: [Tutor] Beginner Question
>Message-ID: <l46e6h$ori$1 at ger.gmane.org>
>Content-Type: text/plain; charset=US-ASCII
>
>On 22/10/2013 10:25, Sven Hennig wrote:
>
>>  Hello, I would like to learn a programming language and have decided to 
use
>> Python. I have some programming experience and doing well in Python. What
>> really causes me problems is OOP.
>> I'm just dont get it... I'm missing a really Practical example. In every
>> book I've read are the examples of such Class Dog and the function is bark. 
Has
>> anyone an OOP example for me as it is really used in real code, so I can
>> better understand the concept? I do not know why this is so hard for me.
>>
>
>What you may not realize is you're already doing OOP, just by using the
>standard library.  When you open a file (or many other things that can
>produce a stream of bytes), you get an instance of class file.  When you
>use that instance, you're calling methods of that instance.  So when you
>say:
>
>infile = open("myfile.txt,"r")
>data = infile.readline()
>
>you're doing object oriented programming.  You don't have to know what
>kind of thing "infile" is, you just have to know it has methods read(),
>readline(), close(), etc.
>
>When you want to write your own classes, or when you want to make a new
>class that's related but different from one of the thousands that are
>standard, that's when it gets interesting.  As Alan says, GUI is one
>place where you'll be wrting your own classes, usually by deriving from
>one of the GUI library classes.
>
>At its most fundamental, a class is a description of how to create and
>how to manipulate instances.  An instance has methods (functions), and
>attributes (data).  When one class is derived from another, it can share
>some or most of the attributes and behavior of the parent class, but
>make changes.  This helps avoid duplicating code when two things are
>similar.
>
>You're familiar with list and tuple.  Those are built-in
>collection classes, supported explicitly by the language. But if you
>want your own collection, you may want to make a class for it.  The Dog
>bark() example may seem silly, but a Dog has lots of other methods
>besides that one, and has lots of attributes (color, breed, health
>state, owner, etc.).  In a sense those attributes are like a list within
>the Dog, but you want them to have nice names, instead of remembering
>that the 3rd one is owner.
>
>
>-- 
>DaveA
>
>
>
>
>------------------------------
>
>Message: 3
>Date: Tue, 22 Oct 2013 19:20:25 +0000
>From: "Key, Gregory E (E S SF RNA FSF 1 C)" <gregory.key at siemens.com>
>To: "tutor at python.org" <tutor at python.org>
>Subject: [Tutor] comma in an assignment
>Message-ID:
>	<5C28117E8FDB0E4F999E3CB30775EF5901F9A5 at USLZUA0EM21MSX.ww017.siemens.net>
>	
>Content-Type: text/plain; charset="us-ascii"
>
>I understand that a comma in Python is a separator and not an operator. In 
some of the MatPlotLib examples I see code like this:
>
>line1, = ax1.plot(t, y1, lw=2, color='red', label='1 HZ')
>
>What does the comma do in an assignment statement?
>
>Greg Key
>
>
>This message and any attachments are solely for the use of intended 
recipients. The information contained herein may include trade secrets, 
protected health or personal information, privileged or otherwise confidential 
information. Unauthorized review, forwarding, printing, copying, distributing, 
or using such information is strictly prohibited and may be unlawful. If you 
are not an intended recipient, you are hereby notified that you received this 
email in error, and that any review, dissemination, distribution or copying of 
this email and any attachment is strictly prohibited. If you have received this 
email in error, please contact the sender and delete the message and any 
attachment from your system. Thank you for your cooperation
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.
org/pipermail/tutor/attachments/20131022/8b7120b2/attachment-0001.html>
>
>------------------------------
>
>Message: 4
>Date: Tue, 22 Oct 2013 23:21:20 +0200
>From: Ulrich Goebel <ml at fam-goebel.de>
>To: tutor at python.org
>Subject: [Tutor] Howto handle pictures with pyqt
>Message-ID: <5266EC50.9070307 at fam-goebel.de>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>Hello,
>
>for my first python program I try to build a user interface for a litle 
>address database. Therefor I use pyqt to build the formular, and later I 
>will connect it to an existing SQLite DB, using APSW.
>
>Besides the "normal" things as name, address, email and others I want to 
>store an image. Normaly that will be given as a file (.jpg, .png, or 
>other type). I would let the user find the picture with a 
>QFileDialog.getOpenFileName() dialog. But what to do after that? Here 
>are my questions:
>
>Which widget is able to show the picture?
>
>How to show the picture? That means: howto put the picture from the file 
>into the widget?
>
>How to put the picture data into an blob(?) column in the database?
>
>How to get the data back from there and bring it in the widget?
>
>May be there is a tutorial which I didn't find yet?
>
>Thank's a lot for any help!
>
>Greetings
>Ulrich
>
>-- 
>Ulrich Goebel
>Paracelsusstr. 120, 53177 Bonn
>
>
>------------------------------
>
>Message: 5
>Date: Tue, 22 Oct 2013 20:18:28 +0200
>From: Sven Hennig <shennig93 at googlemail.com>
>To: tutor at python.org
>Subject: Re: [Tutor] Beginner Question
>Message-ID:
>	<CACCMqHysQF-Bpuf5fRRLnNf+tvZox7dBKjOr8gvHkYAQy3Y+FA at mail.gmail.com>
>Content-Type: text/plain; charset="iso-8859-1"
>
>Thank you! You guys helped me out alot.
>
>@Alan your website is great! Really clearly written. Especially the "Things
>to remember" part.
>
>If you have exercises for me or have a Website with exercises, bring it on. I
>think this is the best way to learn.
>
>
>
>2013/10/22 Dave Angel <davea at davea.name>
>
>> On 22/10/2013 10:25, Sven Hennig wrote:
>>
>> >  Hello, I would like to learn a programming language and have decided to
>> use
>> > Python. I have some programming experience and doing well in Python. What
>> > really causes me problems is OOP.
>> > I'm just dont get it... I'm missing a really Practical example. In every
>> > book I've read are the examples of such Class Dog and the function is
>> bark. Has
>> > anyone an OOP example for me as it is really used in real code, so I can
>> > better understand the concept? I do not know why this is so hard for me.
>> >
>>
>> What you may not realize is you're already doing OOP, just by using the
>> standard library.  When you open a file (or many other things that can
>> produce a stream of bytes), you get an instance of class file.  When you
>> use that instance, you're calling methods of that instance.  So when you
>> say:
>>
>> infile = open("myfile.txt,"r")
>> data = infile.readline()
>>
>> you're doing object oriented programming.  You don't have to know what
>> kind of thing "infile" is, you just have to know it has methods read(),
>> readline(), close(), etc.
>>
>> When you want to write your own classes, or when you want to make a new
>> class that's related but different from one of the thousands that are
>> standard, that's when it gets interesting.  As Alan says, GUI is one
>> place where you'll be wrting your own classes, usually by deriving from
>> one of the GUI library classes.
>>
>> At its most fundamental, a class is a description of how to create and
>> how to manipulate instances.  An instance has methods (functions), and
>> attributes (data).  When one class is derived from another, it can share
>> some or most of the attributes and behavior of the parent class, but
>> make changes.  This helps avoid duplicating code when two things are
>> similar.
>>
>> You're familiar with list and tuple.  Those are built-in
>> collection classes, supported explicitly by the language. But if you
>> want your own collection, you may want to make a class for it.  The Dog
>> bark() example may seem silly, but a Dog has lots of other methods
>> besides that one, and has lots of attributes (color, breed, health
>> state, owner, etc.).  In a sense those attributes are like a list within
>> the Dog, but you want them to have nice names, instead of remembering
>> that the 3rd one is owner.
>>
>>
>> --
>> DaveA
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.
org/pipermail/tutor/attachments/20131022/6f9f40a5/attachment.html>
>
>------------------------------
>
>Subject: Digest Footer
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>https://mail.python.org/mailman/listinfo/tutor
>
>
>------------------------------
>
>End of Tutor Digest, Vol 116, Issue 58
>**************************************
>




More information about the Tutor mailing list