Python Forum
writing list to csv file problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing list to csv file problem
#1
im making a task reminder that will send emails and sms reminding of tasks and events. my problem that im stuck with is with saving the data. i chose csv as it is easy to maintain and construct using the pandas library. the problem is where i am extending my send_date_list even though the variable itself is a list, whilst looking inside the csv file i notice that that cell in question is a list in quotation marks meaning that it writes the value as a string and not a list. obviously i need it to be saved as a list in order to search or delete specific entries. an example of my code follows



def save_info():
    names_list,due_dates_list,send_date_list,end_date_list,desc_list,phone_number_list,email_address_list=[],[],[],[],[],[],[]
    """Save the entered reminder information if valid."""
    try:
        df = pd.read_csv("data.csv")
    except FileNotFoundError:
        with open("data.csv", "w") as file:
            writer = csv.writer(file)
            writer.writerow(["name", "date", "description", "phone number","email address","recurring"])
        df = pd.DataFrame(columns=["name", "date", "description", "phone number","email address","recurring"])
    client=kickbox.Client(api_key)
    kbx=client.kickbox()
    send_date = send_date_ent.get()
    end_date=end_date_ent.get()
    due_date=reminder_date_ent.get()
    to_email_address=email_ent.get()
    response=kbx.verify(to_email_address)
    if response.body['result']!="undeliverable":
        if check_date(send_date):
            if check_date(due_date):
                if check_date(end_date):
                    frequency=simpledialog.askinteger(title="Task frequency",prompt="How many days should the task occur?")

                    name = name_ent.get().title()
                    name_parts = name.split(" ")
                    if len(name_parts) < 2:
                        messagebox.showerror(title="Ooooooops",
                                             message="You have not entered a first and last name!!\nPlease try again!!")
                        return

                    desc = description_ent.get()
                    phone_number = phone_ent.get()
                    pattern=r'^\d{11}'
                    if len(str(phone_number))!=11 or not re.match(pattern,phone_number):
                        messagebox.showerror(title="OOOOOOps", message="You have not entered a valid phone number!!\n"
                                                                       "Please enter a valid UK phone number \n"
                                                                       "The phone number should be 12 digits long!!\n")
                        return
                    if check_alphabetical(name):

                        if check_alphabetical(desc):


                            if messagebox.askyesno(title="Save this information?",
                                                   message=f"On the {due_date} {name_parts[1]}, {name_parts[0]} should be reminded to {desc}!!"
                                                           f"A text message will be sent on {send_date}\n "
                                                           f"Their phone number is {phone_number}\n"
                                                           f"Their email address is {to_email_address}\n"
                                                           f"Do you want to save this information?\n"):
                                saved_data=df.to_dict("records")
                                if var1.get()==1:
                                    end_date_index=dates_list_starting_from_today.index(end_date)
                                    send_date=dates_list_starting_from_today[:end_date_index:frequency]
                                    send_date_list.extend(send_date)
                                    recurring=True
                                    print(send_date_list)
                                else:
                                    due_dates_list.append(due_date)
                                    send_date=send_date_ent.get()
                                    send_date_list.append(send_date)
                                    due_date=reminder_date_ent.get()
                                    recurring=False



                                names_list.append(name)

                                desc_list.append(desc)
                                phone_number_list.append(phone_number)
                                email_address_list.append(to_email_address)
                                saved_data.append({"name":names_list,"send date":send_date_list,"due date":due_dates_list,"description":desc_list,"phone number":phone_number_list,"email address":email_address_list,"recurring":recurring})

                            df2=pd.DataFrame(saved_data)
                            df2.to_csv("data.csv", index=False)

                            send_date_ent.delete(0, END)
                            reminder_date_ent.delete(0,END)
                            name_ent.delete(0, END)
                            description_ent.delete(0, END)
                            phone_ent.delete(0, END)
                            email_ent.delete(0,END)
                            end_date_ent.delete(0,END)
            else:
                messagebox.showerror(title="OOOOOops",
                                             message="You have entered a date set in the past!!\nPlease remedy this mistake and try again!!")
        else:
            messagebox.showerror(title="OOOOOops",
                                         message="You have entered a date set in the past!!\nPlease remedy this mistake and try again!!")
    else:
        messagebox.showerror(title="OOOOOOPs",message="The email address you entered does not exist!!\nPlease correct the mistake and submit again!!\n")
Reply


Messages In This Thread
writing list to csv file problem - by jacksfrustration - Jun-29-2024, 11:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Writing in a document problem IOHANNES 4 1,241 Oct-26-2022, 08:58 PM
Last Post: IOHANNES
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,630 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,118 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Problem with "Number List" problem on HackerRank Pnerd 5 2,303 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Writing to External File DaveG 9 2,828 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  problem writing dataframe to oracle aliyesami 4 2,823 Sep-25-2021, 11:20 PM
Last Post: SamHobbs
  Writing to file ends incorrectly project_science 4 2,934 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 5,032 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,675 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  list from a data file problem Marre 3 2,901 Sep-22-2020, 07:55 AM
Last Post: Marre

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020