Created
February 19, 2024 11:07
-
-
Save AyemunHossain/628dcc1789f2218ed675bd0be2addd45 to your computer and use it in GitHub Desktop.
Convert CSV file to Githubreadme question answer pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
# Read the CSV file | |
def read_csv(file_path): | |
rows = [] | |
with open(file_path, newline='', encoding='utf-8') as csvfile: | |
reader = csv.reader(csvfile) | |
header = next(reader) # Skip header | |
for row in reader: | |
rows.append(row) | |
return rows | |
# Convert CSV rows to Markdown format | |
def csv_to_markdown(rows): | |
markdown_content = "" | |
for i, row in enumerate(rows, start=1): | |
question = row[0] | |
options = row[1:5] | |
answer = row[5] | |
markdown_content += f"**Q{i}:** {question}\n\n" | |
for j, option in enumerate(options, start=1): | |
markdown_content += f"Option {j}: {option}\n" | |
markdown_content += f"\nCorrect Answer: {answer}\n\n---\n\n" | |
return markdown_content | |
# Write Markdown content to a file | |
def write_to_file(markdown_content, output_file): | |
with open(output_file, 'w', encoding='utf-8') as f: | |
f.write(markdown_content) | |
# Main function | |
def main(): | |
input_file = 'input.csv' | |
output_file = 'README.md' | |
rows = read_csv(input_file) | |
markdown_content = csv_to_markdown(rows) | |
write_to_file(markdown_content, output_file) | |
print("README.md file generated successfully!") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment