Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import random
- import string
- def generate_code():
- # Generate 8-character code: 5 letters and 3 numbers in specific positions
- letters = ''.join(random.choices(string.ascii_uppercase, k=5))
- numbers = ''.join(random.choices(string.digits, k=3))
- code = letters[0:2] + numbers[0] + letters[2] + numbers[1] + letters[3] + numbers[2] + letters[4]
- return code
- def generate_study_name(number):
- # Generate study name with leading zeros
- return f"study-{str(number).zfill(7)}"
- # Read and write the CSV file
- with open('my_prolific_submission_history.csv', 'r', encoding='utf-8') as input_file:
- # Read all rows
- rows = list(csv.reader(input_file))
- # Get header row
- header = rows[0]
- # Find the indices of the Study and Completion Code columns
- study_index = header.index('Study')
- completion_code_index = header.index('Completion Code')
- # Process each data row
- study_counter = 1
- for row in rows[1:]: # Skip header row
- # Replace study name if it exists
- if row[study_index].strip():
- row[study_index] = generate_study_name(study_counter)
- study_counter += 1
- # Replace completion code if it exists
- if row[completion_code_index].strip():
- row[completion_code_index] = generate_code()
- # Write the modified data back to a new CSV file
- with open('my_prolific_submission_history_modified.csv', 'w', newline='', encoding='utf-8') as output_file:
- writer = csv.writer(output_file)
- writer.writerows(rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement