检查csv fi中是否已有新条目

2024-10-03 19:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过QT Creator表单接收用户输入。此用户输入被传递给一个函数,该函数将此输入写入CSV文件。一切正常。没问题。你知道吗

但是,当一些新的输入完成后,CSV文件已经包含一行,其中一个特定的“列”等于用户输入中的同一个“列”,那么它应该要么忘记新的条目(用户输入),只修改特定“列”上的CSV文件行,要么从CSV文件中删除旧条目,并将用户输入作为新条目写入输入CSV文件。你知道吗

def check_and_save(newEntry): 

  ### add some new variables to newEntry
  a_count = 0
  b_count = 0
  c_count = 0


  #### what the function should do
  1. check if there is a specific CSV FILE 
     (i.e CSVFILE('StrikeListReport'))

     if the CSV FILE does NOT exist create CSVFILE('StrikeListReport') 
        and write -- new entry -- in a row to it

     else if CSV FILE('StrikeListReport') already exists:

        1. open CSV FILE('StrikeListReport')

        2. go trough each row in CSV FILE('StrikeListReport') and \
           check if --newEntry-- already exist in the 
           CSV FILE('StrikeListReport') based on --new entry[2]-- 
           #(staff_id) 

        3. if --new entry[2] -- already exist in 
           CSV FILE('StrikeListReport')

           the function should either:    

             1. delete the -- old entry -- 
                add (+1) to -- new entry --'s -- a_count -- #new entry[4]
                then write -- new entry -- to CSV FILE('StrikeListReport')

           or :

              2. forget about the --new entry -- 
                 and just add( + int(1) ) to the 
                 -- old entry --'s -- a_count -- # old entry[4] then
                 save / write  CSV FILE('StrikeListReport') with changed 
                 -- old entry --
我的尝试
###### User Input from QT Creator

staff_firstname = "Jane"
staff_lastname = "DoeDoe"
staff_id = "809564"
store_id = "809"
chosen_incident = " just some text "
strike_comment = " just some text "
date = "2012"



class DoIt:
    def save_and_send_strike_report(self, staff_firstname, staff_lastname, staff_id, store_id, chosen_incident,
                                    strike_comment, date):

        strike_count = 0
        detention_count = 0
        hr_penalty_count = 0



        new_strike_report = [staff_firstname, staff_lastname, staff_id, store_id,
                             chosen_incident, strike_count, detention_count, hr_penalty_count, strike_comment,
                             date]

        with open('StrikeListReport' + store_id + '.csv', "r") as csvfile:

            reader = csv.reader(csvfile, delimiter=";")

            for item in reader:

                    for new_item in new_strike_report:

                        if new_item[2] == item[2]:
                            item = item[5] + 1
                            with open('StrikeListReport' + store_id + '.csv', "a") as csvfile_out:
                                writer = csv.writer(csvfile_out)
                                writer.writerow(item)

                        else:
                            with open('StrikeListReport' + store_id + '.csv', "a") as csvfile_out:
                                writer = csv.writer(csvfile_out)
                                writer.writerow(new_item)
bla = DoIt()
bla.save_and_send_strike_report(staff_firstname, staff_lastname, staff_id, store_id, chosen_incident, strike_comment, date)

不管我做了什么,我的输出总是这样。 上面的代码只是我解决问题的方法之一:

  1. _你知道吗csv.错误:应为iterable,而不是int
  2. TypeError:“int”对象不可下标
  3. --它只是将(i)写入我的CSV文件,以“,”分隔
  4. --写无限

Tags: and文件csvthestoreidnewcount