Read data from one csv and create another modified csv file
<p>I have a csv file called ipValues.csv which contains the following data:</p>

<pre><code>IPs             Values  
192.168.1.231   c3s8b1p1    c3s8b1p2    c3s4b1p3        
192.168.1.179   c1s1b1p1    c1s1b1p2    c1s3b1p1    c3s2b1p2    c3s2b1p3
192.168.1.195   c1s1b2p8                
192.168.1.162   c1s4b7p8    c1s1b1p3    c1s1b2p2    c1s1b2p3    c1s1b2p5
192.168.1.179   c1s1b1p1    c1s1b1p2    c1s3b1p1    c3s2b1p2    c3s2b1p3
192.168.1.143   c2s4b1p2    c2s2b1p3    c2s2b1p5    c2s2b1p9    
192.168.1.231   c3s8b1p1    c3s8b1p2    c3s4b1p3    c3s7b1p6    c2s2b1p1
192.168.1.187   c5s4b1p2    c4s9b1p3            
192.168.1.114   c1s1b1p10   c1s3b6p1    c1s1b10p9   c4s10b1p1   
192.168.1.132   c1s1b2p1    c1s10b7p8   c4s9b1p1    c4s9b1p2    c3s6b1p3
192.168.1.164   c1s1b1p5    c1s1b1p9    c1s1b1p8    c1s1b2p2    c3s5b1p2
</code></pre>

<p>I want to create another csv file in the following format:</p>

<pre><code>values    Ips
c3s8b1p1  192.168.1.231
c3s8b1p2  192.168.1.231
c3s4b1p3  192.168.1.231
c1s1b1p1  192.168.1.179
c1s1b1p2  192.168.1.179
c1s3b1p1  192.168.1.179
c3s2b1p2  192.168.1.179
c3s2b1p3  192.168.1.179
</code></pre>

<p>and so on...</p>

<p>I know guys this is very difficult to understand but I don't know how can I explain this, Sorry for that. Please give me suggestion. </p>

<p>The following should get you started and help explain how to do things for use on a small file:</p>

<pre><code>import csv

with open('ipValues.csv', 'rb') as f_input, open('output.csv', 'wb') as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)

    next(csv_input)     # skip header
    csv_output.writerow(["values", "Ips"])

    for row in csv_input:
        for entry in row[1:]:
            csv_output.writerow([entry, row[0]])
</code></pre>

<p>Giving you a CSV output file as follows:</p>

<pre><code>values,Ips
c3s8b1p1,192.168.1.231
c3s8b1p2,192.168.1.231
c3s4b1p3,192.168.1.231
c1s1b1p1,192.168.1.179
c1s1b1p2,192.168.1.179
c1s3b1p1,192.168.1.179
c3s2b1p2,192.168.1.179
c3s2b1p3,192.168.1.179
c1s1b2p8,192.168.1.195
c1s4b7p8,192.168.1.162
c1s1b1p3,192.168.1.162
c1s1b2p2,192.168.1.162
c1s1b2p3,192.168.1.162
c1s1b2p5,192.168.1.162
c1s1b1p1,192.168.1.179
c1s1b1p2,192.168.1.179
c1s3b1p1,192.168.1.179
c3s2b1p2,192.168.1.179
c3s2b1p3,192.168.1.179
c2s4b1p2,192.168.1.143
c2s2b1p3,192.168.1.143
c2s2b1p5,192.168.1.143
c2s2b1p9,192.168.1.143
c3s8b1p1,192.168.1.231
c3s8b1p2,192.168.1.231
c3s4b1p3,192.168.1.231
c3s7b1p6,192.168.1.231
c2s2b1p1,192.168.1.231
c5s4b1p2,192.168.1.187
c4s9b1p3,192.168.1.187
c1s1b1p10,192.168.1.114
c1s3b6p1,192.168.1.114
c1s1b10p9,192.168.1.114
c4s10b1p1,192.168.1.114
c1s1b2p1,192.168.1.132
c1s10b7p8,192.168.1.132
c4s9b1p1,192.168.1.132
c4s9b1p2,192.168.1.132
c3s6b1p3,192.168.1.132
c1s1b1p5,192.168.1.164
c1s1b1p9,192.168.1.164
c1s1b1p8,192.168.1.164
c1s1b2p2,192.168.1.164
c3s5b1p2,192.168.1.164
</code></pre>

<p>This was tested using Python 2.7. It uses Python's <a href="https://docs.python.org/2/library/csv.html#module-csv" rel="nofollow"><code>csv</code></a> library to parse and create the CSV entries in your files.</p>

python csv