Skip to content

Latest commit

 

History

History
60 lines (28 loc) · 5 KB

ckotm4zts02s9tys124m1hsj4.md

File metadata and controls

60 lines (28 loc) · 5 KB

How to parse .csv with Python

%[https://github.com/W01fw00d/bondy]

Python in Science

Python 🐍 is a great language for beginners 👶 (I myself started coding with it). It's especially useful for creating small tasks ⚙ on your computer, automating file management 📂, calculations 🧮, document formatting 📄...

So you won't be surprised to know that it's the preferred language among scientists 👩🏻‍🔬. A lot of artificial intelligence libraries are written in Python, but other science fields like Chemistry 🧪 use Python in their calculus programs, like the R project.

Solving a problem

Let me share a little example with you. One day, a chemist (who happens to be also my wife 😇) asked me how to transform a concrete .csv 📃 columns and rows structure into a different structure automatically.

The UV-vis spectrophotometer machine ⚙ my wife was using at the university laboratory was outputting a format "A", and she needed a format "B" as input for the analysis software 👨🏼‍💻(ReactLab JPlus Consulting) that was processing the scientific data.

%[https://media.giphy.com/media/l0HlQCEq4A9H2evVC/giphy.gif]

How to decide when to automate a task

She has been doing that task manually for a week 📅, realizing that all that time was being wasted on something that should be trivial. Time ⏳ that she could have been investing in doing actual scientific work.

In addition to that, she wasn't the only one who had to suffer this manual process: the machine was used by other colleagues 🥼 in the present, and potentially, in the future.

This kind of situation is ideal for creating a task and make somebody more productive and probably happier 🙂. To be sure about it, just sum the hours 🕗 each person is wasting on a task daily, and multiply it by all the persons 👨‍👨‍👧‍👧 in that situation (and consider persons outside the organization that could benefit from it too! 🌐). If the resulting number is bigger than the hours of effort 💦 you will need to invest, it's generally a good idea to develop that task ✅.

So I asked my wife 💁🏽‍♀️ an example for the input.csv and the output.csv and started working!

%[https://media.giphy.com/media/pM0cqxUmZqkow/giphy.gif]

Some details

Python includes by default:

1- A csv module 🧩 that gave me the feature I needed to write the output.csv.

2- A glob module 🧩 that allowed me to read the input.csv

So, I just needed to develop the logic 🦾 for rearranging and restructuring the columns and row!

Finally, I just wrote a small batch file that could be clicked 🖱 by the user so they didn't need to use their OS terminal.

Closing time

I named the task "Bondy" as a reference to the concept of covalent bonds ⛓ in chemistry.

I tried to keep the README very simple so people with no programming experience could use the ask without issues 😊. The harder part of the setup was installing Python, luckily MacOS includes it by default and Windows users can just download and install it without needing more than user-level computer knowledge 😎...

%[https://media.giphy.com/media/OeyAkKTKYSvmw/giphy.gif]

This task helped to publish this research paper 📃, which makes me quite happy as an old hard-core Science supporter 💓

If you are a scientist 👨🏿‍🔬, I think you could benefit a lot from learning a bit of Python to create your own small tasks. If you combine that skill with some spreadsheets 📄 skills, you will be able to work faster and be more independent. Free your mind from repetitive work, learn to code today!

Did you solve any similar problems with some lines of code? Did it blow your mind 🤯? Let me know in the comments 😉.