Python check if value in csv file
<p>i got list of URLs, for example: </p>

<pre><code>urls_list = [
    "http://yandex.ru",
    "http://google.ru",
    "http://rambler.ru",
    "http://google.ru",
    "http://gmail.ru",
    "http://mail.ru"
]
</code></pre>

<p>I need to open the csv file, check if each value from list in file - skip to next value, else (if value not in a list) add this value in list. </p>

<p>Result: 1st run - add all lines (if file is empty), 2nd run - doing nothing, because all elements in already in file. </p>

<p>A wrote code, but it's work completely incorrect:</p>

<pre><code>import csv


urls_list = [
    "http://yandex.ru",
    "http://google.ru",
    "http://rambler.ru",
    "http://google.ru",
    "http://gmail.ru",
    "http://mail.ru"
]



with open('urls_list.csv', 'r') as fp:
    for row in fp:
        for url in urls_list:
            if url in row:
                print "YEY!"
            with open('urls_list.csv', 'a+') as fp:
                wr = csv.writer(fp, dialect='excel')
                wr.writerow([url])
</code></pre>

<p>Read the file <em>into</em> a variable-</p>

<pre><code>with open('urls_list.csv', 'r') as fp:
    s = fp.read()
</code></pre>

<p>Check to see if each list item is in the file, if not save it</p>

<pre><code>missing = []
for url in urls_list:
    if url not in s:
        missing.append(url + '\n')
</code></pre>

<p>Write the missing url's to the file</p>

<pre><code>if missing:
    with open('urls_list.csv', 'a+') as fp:
        fp.writelines(missing)
</code></pre>

python loops csv if-statement