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>Considering your file has only one column, the <code>csv</code> module might be an overkill. </p>

<p>Here's a version that first reads all the lines from the file and reopens the file to write urls that are not already in the file:</p>

<pre><code>lines = open('urls_list.csv', 'r').read()

with open('urls_list.csv', 'a+') as fp:
    for url in urls_list:
        if url in lines:
            print "YEY!"
        else:
            fp.write(url+'\n')
</code></pre>

python loops csv if-statement