Merging all rows with common value
<p>Here's what I want to do using Python:</p>

<p><code>file1.csv</code> contains:</p>

<pre><code>Code,Expenditure
1,Meal
2,Taxi
3,Apartment
4,Laundry
</code></pre>

<p><code>file2.csv</code> contains:</p>

<pre><code>Code,Amount
1,150
2,90
2,100
2,85
3,5000
</code></pre>

<p>Now I want to merge them into another file (<code>output.csv</code>) that will look like this:</p>

<pre><code>Code,Expenditure,Amount
1,Meal,150
2,Taxi,90
2,Taxi,100
2,Taxi,85
3,Apartment,5000
4,Laundry,
</code></pre>

<p>Any help or suggestions will be greatly appreciated!</p>

<p>Read about file handling and python basics. As @Scott hunter told, you should try something, if you face any problems you can ask your question with your code here.</p>

<p>Anyway, <strong>Here is what you need,</strong></p>

<pre><code>a=open('a.txt','r').readlines()
b=open('b.txt','r').readlines()
for i in a:
    i=i.rstrip().split(',')
    for j in b:
         j=j.rstrip().split(',')
         if i[0]==j[0]:
             A=i[0],i[1],j[1]
             print ' '.join(A)
</code></pre>

python csv merge