Python Copying specific rows from a .csv to an .xlsx given that a particular value is in the third column
<p>I am new to python. I have done a very small amount at Uni.
I am working on a personal program for my family that takes .csv files from an ftp site, merges them all together, and then puts the information into a template .xlsx file. the information in this csv consist of each row holding a set of data for a position of a node. </p>

<p>I am stuck with this last section, moving the information from the csv file to the xlsx file.</p>

<p>the following image is of a bit of the csv file:
<a href="http://i.stack.imgur.com/6mST3.png" rel="nofollow">http://i.stack.imgur.com/6mST3.png</a></p>

<p>the third column (labelled valve pos) will have multiple rows from 1 - 15 and it will repeat (so there will be more than one row with a 1 in that column, so on).
I need to copy all the rows with a 1 in this column into a pre-existing sheet in the xlsx template. however other columns may also have a 1 without having a 1 in the third column so it needs to be based off that column alone. I have tried a couple of ways but keep hitting errors or ending up with corrupted information in the xlsx (eg merging of all the rows into one single row, the columns being in different positions, so on)</p>

<p>this so far includes the code I am currently working with these are two different codes i have tried but have not had success with.</p>

<p>The first code is:</p>

<pre><code>wb = openpyxl.load_workbook('MasterTemplate4.xlsx')
    ws = wb.get_sheet_by_name('All')
    with open('file location' + fl1 + '.csv') as f:
        reader = csv.DictReader(f)
        rows = [row for row in reader if row['Valve Pos'] != '1']

    for row in rows:
        ws.write(row)

    wb.save('save location' + fl1 + '.xlsx')
</code></pre>

<p>The second code is:</p>

<pre><code>    f = open(r'file location' + fl1 + '.csv')

    csv.register_dialect('colons', delimiter=':')

    reader = csv.reader(f, dialect='colons')

    wb = openpyxl.load_workbook('MasterTemplate4.xlsx')
    ws = wb.get_sheet_by_name('All')

    for row_index, row in enumerate(reader):
        for column_index, cell in enumerate(row):
            column_letter = get_column_letter((column_index + 1))
            ws.cell('%s%s'%(column_letter, (row_index + 1))).value = cell

    wb.save('save location' + fl1 + '.xlsx')
</code></pre>

<p>The imports and defs have been done up the top globally as they are used in other sections of code</p>

<p>any help would be appreciated as I have been stuck on this for some weeks now, and have been unable to find any code online to deal with this issue.</p>