How to use ChatGPT to write Python code

How to use ChatGPT to write Python code

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:

  1. read the file with the list of numbers
  2. create a folder for each file, the name of which corresponds to the title of the number
  3. create a markdown file in each newly created folder
  4. insert the introduction into this file
  5. read the file with the list of contents
  6. 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.

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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.

要查看或添加评论,请登录

Marco Cevoli的更多文章

  • Dopo la partita...

    Dopo la partita...

    Ciao, scrivo questo numero al volo, di ritorno dall'ennesima sconfitta della "Penya", la principale squadra di basket…

  • Inktober, cambiamento climatico e loghi d'auto

    Inktober, cambiamento climatico e loghi d'auto

    Leggi l'ultimo numero de I deliri diari qui: https://ideliridiari.curated.

    1 条评论
  • Arriva l'autuno, ma sono ancora in t-shirt

    Arriva l'autuno, ma sono ancora in t-shirt

    Lo sai, non mi va di fare copia-incolla. Se vuoi leggere il numero 43 della newsletter vai su Curated:…

    1 条评论
  • Inizia la scuola...

    Inizia la scuola...

    ..

    1 条评论
  • Settembre alle porte

    Settembre alle porte

    Ciao, ho saltato qualche numero per pura pigrizia estiva. Torno con i consueti dieci link che hanno richiamato la mia…

  • Fine luglio, fine batterie

    Fine luglio, fine batterie

    Non so tu, ma io sono veramente stanco. Sarà.

    2 条评论
  • Caldo torrido...

    Caldo torrido...

    ..

    1 条评论
  • Cose che non funzionano e barche che si rovesciano

    Cose che non funzionano e barche che si rovesciano

    Ciao, di cose che non funzionano ce ne sono tante, lo so, ma qui mi riferisco a un intrigante elenco compilato da…

  • Europei e Neanderthal

    Europei e Neanderthal

    Ciao, no, il riferimento ai Neanderthal non è un'offesa politicamente scorretta rivolta all'imbarazzante prestazione…

  • Trapianto di testa, maschera da scimmia

    Trapianto di testa, maschera da scimmia

    Avviso: in fondo alla newsletter ci sono due video un po' inquietanti. Niente di peggio di quello che si vede…

社区洞察

其他会员也浏览了