title: python program Remove duplicate string with sequence of number without using pandas DataFrame
body: <p><strong>Replace a csv file duplicated string with sequence of numbers WITHOUT using PANDAS DATAFRAME</strong> </p>
  
  <blockquote>
    <p>python
    I have a csv file that contain 4 column i want to replace a string of all the column with a sequence of number if any duplicate will be there then it should give the prevoius number.for that i have written this code which return a three dicts:dict1,dict2,dict3 now i want to print that dictionary value in a file like given in below fig.</p>
  </blockquote>
  
  <pre><code>with open(tempFile, 'r', encoding="utf8") as csvfile: 
      # creating a csv reader object 
          csvreader = csv.reader(csvfile, delimiter=',')
          next(csvreader, None)
          firstRow = next(csvreader)
  
          NameCount = 1
          AddressCount=1
          EmailCOunt=1
          input_dict={firstRow[1]:NameCount}
          input_dict2={firstRow[2]:AddressCount}
          input_dict3={firstRow[3]:EmailCOunt}
  
          dict1={firstRow[0]:NameCount}
          dict2={firstRow[0]:AddressCount}
          dict3={firstRow[0]:EmailCOunt}
  
          for row in csvreader:
  
              value = input_dict.get(row[1])
  
              if value is None:
                  NameCount = NameCount + 1
                  input_dict.update({row[1]:NameCount})
                  dict1.update({row[0]: NameCount})
  #                 input_dict2.update({row[2]:counter})
  #                 dict3.update({row[0]: counter})
  
              else:
                  input_dict.update({row[1]: value})
                  dict1.update({row[0]: value})
  #             
  #                 input_dict2.update({row[2]: value1})
  #                 dict3.update({row[0]: value1})
  #             value = input_dict2.get(row[2])
  
              value1 = input_dict2.get(row[2])
              if value1 is None:
                  AddressCount = AddressCount + 1
                  input_dict2.update({row[2]:AddressCount})
                  dict2.update({row[0]: AddressCount})
              else:
                  input_dict2.update({row[2]: value1})
                  dict2.update({row[0]: value1})
  
              value2 = input_dict3.get(row[3])
              if value2 is None:
                  EmailCOunt = EmailCOunt + 1
                  input_dict3.update({row[3]:EmailCOunt})
                  dict3.update({row[0]: EmailCOunt})
              else:
                  input_dict3.update({row[3]: value2})
                  dict3.update({row[0]: value2})
  
          print('dict1-', dict1)
          print('dict2-', dict2)
          print('dict3-', dict3)[this is the image of my input csv file in which i have replaced the duplicated string of col 1,2,3 with seq no. by using dicts[this is how i need my output look like after string replacement][1]  ][1]
  </code></pre>
  
  <p>This is the input data which is written in csv file:</p>
  
  <pre><code>job_Id  Name        Address     Email
  1   snehil singh    marathalli  ss@gmail.com
  2   salman      marathalli  ss@gmail.com
  3   Amir        HSR     ar@gmail.com
  4   Rakhesh     HSR     rakesh@gmail.com
  5   Ram     marathalli  r@gmail.com
  6   Shyam       BTM     ss@gmail.com
  7   salman      HSR     ss@gmail.com
  8   Amir        BTM     ar@gmail.com
  9   snehil singh    Majestic    sne@gmail.com
  </code></pre>
  
  <p>and the required ouput which i'm not able to get is:</p>
  
  <pre><code>job_Id  Name    Address Email
  1          1       1    1
  2          2       1    1
  3          3       2    2
  4          4       2    3
  5          5       1    4
  6          6       3    1
  7          2       2    1
  8          3       3    2
  9          1       4    5
  </code></pre>
  
  <p>please help........</p>
  
  <p>Hello guys i tried with this way its working..</p>
  
  <pre><code>count=1
              iter_obj1 = iter(dict1.values())
              iter_obj2= iter(dict2.values())
              iter_obj3 = iter(dict3.values())
              while True:
                  try:
                      element1 = next(iter_obj1)
                      element2 = next(iter_obj2)
                      element3 = next(iter_obj3)
                      s = count, element1, element2, element3
                      print(s)
  
                      with open("snehil.csv", 'w') as f:
                          f.write('\n')
                          f.write(json.dumps(s)+'\n')
                          f.write(line)
                      count=count +1
                  except StopIteration:
                      break
  </code></pre>
  
  <p>and the output is :</p>
  
  <pre><code>(1, 1, 1, 1)
  (2, 2, 1, 1)
  (3, 3, 2, 2)
  (4, 4, 2, 3)
  (5, 5, 1, 4)
  (6, 6, 3, 1)
  (7, 2, 2, 1)
  (8, 3, 3, 2)
  (9, 1, 4, 5)
  </code></pre>
  
  <p>This is the correct output but i'm not able to print it in csv file its only showing the last row (9, 1, 4, 5) its means its reading all the data in single line..for print this i have used: </p>
  
  <pre><code>with open("snehil.csv", 'w') as f:
  #f.write('\n')
  f.write(json.dumps(s)+'\n')
  </code></pre>
  
  <p>even i tried it with Dataframe also to print it into csv file but give a error like: AttributeError: 'tuple' object has no attribute 'values'
  for dataframe i have written like:</p>
  
  <pre><code>df=pd.DataFrame.from_dict(s, orient='index')
  print(df)
  </code></pre>
  
  <p>please help me how to get it in csv file and print all the rows in differnt cell...thanks</p>
  
  <p><strong>Program to read the csv file and replace the string with squence of number and write it to csv file</strong> </p>
  
  <pre><code>import csv
  import os 
  from io import StringIO
  # tempFile="input1.csv"
  
  with open("input1.csv", 'r') as csvfile: 
      # creating a csv reader object 
          reader = csv.reader(csvfile, delimiter=',')
          next(reader, None)
  
          data = {}
          for row in reader:
              for header, value in row.items():
                  try:
                      data[header].append(value)
                  except KeyError:
                      data[header] = [value]
  
          for key in data.keys():
              values = data[key]
  
              things = list(sorted(set(values), key=values.index))
  
              for i, x in enumerate(data[key]):
                  data[key][i] = things.index(x) + 1
  
          with open("snehil.csv", "w") as outfile:
              writer = csv.writer(outfile)
              # Write headers
              writer.writerow(data.keys())
              # Make one row equal to one value from each list
              rows = zip(*data.values())
              # Write rows
              writer.writerows(rows)  
  </code></pre>
  
  <p>while excuting this program i got an error: </p>
  
  <pre><code>for header, value in row.items():
  AttributeError: 'list' object has no attribute 'items'
  </code></pre>
  
  <p>please help me i don't understand why i got this error......</p>