检查csv文件中是否已存在新条目

时间:2019-06-17 11:37:12

标签: python python-3.x csv

我通过QT Creator表单接收用户输入。该用户输入将传递给将输入写入CSV FILE的函数。一切正常。没问题。

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

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.Error:可迭代,不是整数
  2. TypeError:“ int”对象不可下标
  3. -它只将(i)写到我的CSV文件中,并以“,”分隔
  4. -无限写入

0 个答案:

没有答案