Advertisement
Trai60

modify_codes.py

Nov 30th, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import csv
  2. import random
  3. import string
  4.  
  5. def generate_code():
  6. # Generate 8-character code: 5 letters and 3 numbers in specific positions
  7. letters = ''.join(random.choices(string.ascii_uppercase, k=5))
  8. numbers = ''.join(random.choices(string.digits, k=3))
  9. code = letters[0:2] + numbers[0] + letters[2] + numbers[1] + letters[3] + numbers[2] + letters[4]
  10. return code
  11.  
  12. def generate_study_name(number):
  13. # Generate study name with leading zeros
  14. return f"study-{str(number).zfill(7)}"
  15.  
  16. # Read and write the CSV file
  17. with open('my_prolific_submission_history.csv', 'r', encoding='utf-8') as input_file:
  18. # Read all rows
  19. rows = list(csv.reader(input_file))
  20.  
  21. # Get header row
  22. header = rows[0]
  23.  
  24. # Find the indices of the Study and Completion Code columns
  25. study_index = header.index('Study')
  26. completion_code_index = header.index('Completion Code')
  27.  
  28. # Process each data row
  29. study_counter = 1
  30. for row in rows[1:]: # Skip header row
  31. # Replace study name if it exists
  32. if row[study_index].strip():
  33. row[study_index] = generate_study_name(study_counter)
  34. study_counter += 1
  35.  
  36. # Replace completion code if it exists
  37. if row[completion_code_index].strip():
  38. row[completion_code_index] = generate_code()
  39.  
  40. # Write the modified data back to a new CSV file
  41. with open('my_prolific_submission_history_modified.csv', 'w', newline='', encoding='utf-8') as output_file:
  42. writer = csv.writer(output_file)
  43. writer.writerows(rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement