Home > other >  I want to open the csv if file already created in that location with same file name otherwise create
I want to open the csv if file already created in that location with same file name otherwise create

Time:10-05

I want to open the CSV if a file is already created in that location with the same file name otherwise create on new CSV to record my scrape data with headings but If the file has already been created it should record only data not repeat headings again on the same CSV file in python code to scrape and save data from multiple articles and save in one CSV.

# =============== Data Store                      
Data = [[Category,Headlines,Author,Source,Published_Date,Feature_Image,Content,url]]
try:
    df = pd.DataFrame (Data, columns = ['Category','Headlines','Author','Source','Published_Date','Feature_Image','Content','URL'])
    print(df)
    with open('C:/Users/Public/pagedata.csv', 'a') as f:
        df.to_csv(f, header=False)
except:
    df = pd.DataFrame (Data, columns = ['Category','Headlines','Author','Source','Published_Date','Feature_Image','Content','URL'])
    print(df)
    df.to_csv('C:/Users/Public/pagedata.csv', header=True)

CodePudding user response:

You can divide code to two sections in try/except. First you try to check if file with db exists - if yes, in the same moment you can concatenate new data to old data. If not, you are just creating new data(headers are True on default).

cols = ['Category','Headlines','Author','Source','Published_Date','Feature_Image','Content','URL']
try:
    opened_df = pd.read_csv('C:/Users/Public/pagedata.csv')
    opened_df = pd.concat([opened_df,pd.DataFrame(Data, columns = cols)])
except:
    opened_df = pd.DataFrame(Data, columns = cols)

opened_df.to_csv('C:/Users/Public/pagedata.csv')
  • Related