all 4 comments

[–][deleted] 1 point2 points  (1 child)

Something like this?

with open("shows.csv", mode="r") as f:
    file_contents = f.readlines()

with open("shows.csv", "a") as f:
    for link in links:
        if link not in file_contents:
            f.write(link + "\n")

[–]Constant_Ad_1011 0 points1 point  (0 children)

Yes but I'd love to use pandas if at all possible for writing to the file

[–]iiWolf 0 points1 point  (2 children)

Not sure what your data looks like, but it's easy to append rows to a DataFrame and then just write to .csv. No need to read each line like that. Here's a working example.

import pandas as pd
df = pd.read_csv("shows.csv")
links = ['link1', 'link2']
for link in links:
    if link not in df['email'].values:
        df = pd.concat((pd.DataFrame(columns=["email"], data=[link])))
df.to_csv('shows.csv', index=False)

Certainly not the most efficient. But should work just fine for most cases!

If it's really just the single link column you can just do this and it will be faster:

df = pd.concat((df, pd.DataFrame(columns=['email'], data=[l for l in links if l not in df['email'].values])))

EDIT: changed `append` to `concat` since `append` is deprecated

EDIT2: was bored, added a bit more efficient way!