Help with CSV editing


I have search far and wide for an example of how to edit / rewrite a CSV based on some simple conditions and some string concatenation. I realised that it may have to be written into a new CSV and this is fine.

Here’s the format of the CSV with some fake data

CSV format

FlatNumber FirstName Surname Email
1 Darth Vader darth.vader@madeup.domain
1.1 Luke Skywalker luke.skywalker@madeup.domain
2 Ben Kinobe ben.kinobe@madeup.domain
2.1 Leia Organa Leia.Organa@madeup.domain
2.2 Han Solo han.solo@madeup.domain
3 Jabba Hutt jabba.hutt@madeup.domain
3.1 Lando Calrissian lando.calrissian@madeup.domain
3.2 Wedge Antilles wedge.antilles@madeup.domain
4 Emperor Palpatine emperor.palpatine@madeup.domain
4.1 Mon Mothma mon.mothma@madeup.domain
5 C- 3PO C-3PO@madeup.domain

What I’d like to do is delete certain emails by looping through and comparing to a string array? Say you always wanted to remove wedge.antilles@madeup.domain & mon.mothma@madeup.domain

The next thing I’d like to do would be to merge flat number, firstname, surname and concatenate them “add flat"flatnumber firstname”." surname “” resulting in

add flat1 luke.skywalker@madeup.domain

For various reasons I can’t use Pandas to achieve this and wondered if Python could do it without add-on functionality.

Thanks in advance for any guidance!

Sure you can do it without Pandas, but… well it depends on the data-structure you are turning the CSV into.
If it is just a string, you can use .split() in loops to turn it into an array of arrays, keep the very first as index while designing loops to go through the others.

Like, you first split it at linebreaks, then at spaces.
Regular Expressions might be helpful for certain operations again, but then you’d need to import re. Generally you should look into built-in string-operations in Python, those can already do a lot.
But in theory, every library you import could also be written directly in Python. It will be less efficient for various reasons, but on such a low scale, that can be neglected.

Thanks Jagaya,

I’m not worried about efficiency, it doesn’t have to be quick or scale, just wanted to know if what functionality I want is possible in Python because virtually all web searched end in “First install Pandas”

So you propose I load the CSV data into arrays and then process the data with .split()?

Ehm no, I meant to load it in as string and turn it into arrays via .split().
I don’t know how native Python loads in pure csv-data, I just assumed it would read the text as a pure string.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.