Python CSV: How to ignore writing similar rows given one row meets a condition?
<p>I'm currently keeping track of the large scale digitization of video tapes and need help pulling data from multiple CSVs. Most tapes have multiple copies, but we only digitize one tape from the set. I would like to create a new CSV containing only tapes of shows that have yet to be digitized. Here's a mockup of my original CSV:</p>

<pre><code>Date Digitized |  Series  | Episode Number | Title | Format
---------------|----------|----------------|-------|--------
01-01-2016     | Series A |       101      |       |  VHS
               | Series A |       101      |       |  Beta
               | Series A |       101      |       |  U-Matic
               | Series B |       101      |       |  VHS
</code></pre>

<p>From here, I'd like to ignore all fields containing "Series A" AND "101", as this show has a value in the "Date Digitized" cell. I attempted isolating these conditions but can't seem to get a complete list of undigitized content. Here's my code:   </p>

<pre><code>import csv, glob

names = glob.glob("*.csv")
names = [os.path.splitext(each)[0] for each in names]

for name in names:
    with open("%s_.csv" % name, "rb") as source:
        reader = csv.reader( source )
        with open("%s_edit.csv" % name,"wb") as result:
            writer = csv.writer( result )
            for row in reader:
                if row[0]:
                    series = row[1]
                    epnum = row[2]
                if row[1] != series and row[2] != epnum:
                    writer.writerow(row)
</code></pre>

<p>I'll add that this is my first question and I'm very new to Python, so any advice would be much appreciated! </p>

<p>I am not a hundred percent sure I've understood your needs. However, this might put you on a right track. I am using <a href="http://pandas.pydata.org/pandas-docs/stable/index.html" rel="nofollow"><code>pandas</code></a> module:</p>

<pre><code>data = """
Date Digitized |  Series  | Episode Number | Title | Format
---------------|----------|----------------|-------|--------
01-01-2016     | Series A |       101      |       |  VHS
               | Series A |       101      |       |  Beta
               | Series A |       101      |       |  U-Matic
               | Series B |       101      |       |  VHS"""
# useful module for treating csv files (and many other)
import pandas as pd
# module to handle data as it was a csv file
import io
# read the csv into pandas DataFrame
# use the 0 row as a header
# fields are separated by |
df = pd.read_csv(
    io.StringIO(data),
    header=0,
    sep="|"
)
# there is a bit problem with white spaces
# remove white space from the column names
df.columns = [x.strip() for x in df.columns]
# remove white space from all string fields
df = df.applymap(lambda x: x.strip() if type(x) == str else x)
# finally choose the subset we want
# for some reason pandas guessed the type of Episode Number wrong
# it should be integer, this probably won't be a problem when loading 
# directly from file
df = df[~((df["Series"] == "Series A") &amp; (df["Episode Number"] == "101"))]
# print the result
print(df)
#     Date Digitized      Series    Episode Number    Title    Format
# 0  ---------------  ----------  ----------------  -------  --------
# 4                     Series B               101                VHS
</code></pre>

<p>Feel free to ask, hopefully I'll be able to change the code according to your actual needs or help in any other way.</p>

python csv