Konstantin Rusanov
Konstantin Rusanov

Reputation: 6554

Python check if value in csv file

i got list of URLs, for example:

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

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.

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

A wrote code, but it's work completely incorrect:

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])

Upvotes: 1

Views: 20590

Answers (2)

Moses Koledoye
Moses Koledoye

Reputation: 78546

Considering your file has only one column, the csv module might be an overkill.

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:

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')

Upvotes: 1

wwii
wwii

Reputation: 23743

Read the file into a variable-

with open('urls_list.csv', 'r') as fp:
    s = fp.read()

Check to see if each list item is in the file, if not save it

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

Write the missing url's to the file

if missing:
    with open('urls_list.csv', 'a+') as fp:
        fp.writelines(missing)

Upvotes: 5

Related Questions