How to use ChatGPT to write Python code
Marco Cevoli
We translate content into 15 languages. Serving ecommerce & B2B brands with technically challenging requirements (Moodle, LaTeX, InDesign, CMSs, etc.) - More languages = more international sales.
For a while, from 8 April 2021 until 7 December 2022, I sent out a newsletter to a handful of brave people who volunteered to receive it.
I used a newsletter publishing system called Revue, which was later acquired by Twitter and then closed down.
Closed down. Gone.
And with it all the newsletter issues.
Luckily enough, before closing down they gave me the option to download the content offline, which I promptly did.
However... the file with the back issues was not exactly user-friendly. It was a zipped file with various files, including:
- a CSV file with the list of subscribers (name and email)
- a CSV file with the list of issues and the introductory text of each issue
- a JSON file with all the paragraphs that made up each issue
Looking for alternatives, I first tried to import these files into Substack, but with lame results: the numbers were imported more or less correctly (without images and without previews of any kind), but all the introductory texts were missing.
Then I tried Beehiiv, another system for publishing and sharing newsletters, but the only way to import a previous newsletter created with Revue was to give Beehiiv the URL of the Revue page. But Revue closed down. Duh.
For a while I let it go, then - stubbornly - I thought: what if I wrote a little program in Python to convert the content into Markdown files to upload to my site? My personal site, www.marcocevoli.com is designed with Grav CMS and its pages are simple markdown files.
So I picked up the book Automate the boring stuff with Python, with which I managed to write a rough algorythm:
- read the file with the list of numbers
- create a folder for each file, the name of which corresponds to the title of the number
- create a markdown file in each newly created folder
- insert the introduction into this file
- read the file with the list of contents
- insert the contents in the markdown files, each in the corresponding number and in the right order
Then I thought: "Why go crazy here? I'll have ChatGPT write the programme!"
So I went to the OpenAI website and asked ChatGPT:
I need a Python program that takes a CSV file and outputs a markdown file, one for each row in the CSV file. Then I need to add the content of a JSON file to each generated markdown. Wait for me to give you sample files before giving me the solution.
Then I extracted a few fragments from the two main files, the CSV with the numbers and the JSON with all the contents, and created two smaller files with which to experiment. I submitted them to ChatGPT.
Initially I made a mistake, writing 'Markdown' instead of 'JSON', while giving a JSON file as an example.
领英推荐
So ChatGPT gave me an obviously wrong solution. After apologising :) I asked him to correct the code and he promptly gave me the first version of the programme:
I verified the programme by running it on my computer: it worked.
However, it was only a skeleton. Some important functions were missing.
I gradually asked the chatbot for them, each time running the programme with the two input files.
On a couple of occasions, errors occurred: I asked ChatGPT what they meant and it explained it to me, as in this example:
Attempt after attempt, I refined the programme more and more, even making some manual changes to the code provided by ChatGPT.
ChatGPT 'remembers' what is said in the conversation, so it is easy to request changes to a piece of code without having it rewrite the rest, as in this case, where I asked it to create folders without spaces and without non-ASCII characters:
Conclusion?
In a few hours, due in part to my lack of experience with Python, I put together a fully-functional programme that in seconds converted a year's worth of Revue newsletters into a series of Markdown articles ready to upload to my personal blog.
The differences between the original version of the newsletter and the issues published on marcocevoli.com are due to the lack of images (which were saved on Revue's servers and therefore no longer accessible), the lack of any previews of non-YouTube videos (because I didn't want to manually fix them), and the somewhat abstruse way in which the content was saved by Revue within the JSON file.
Otherwise, all that content is now at home.
Note: Article originally written in Italian and published on marcocevoli.com.