Python Forum

Full Version: Add column to csv file based on values in existing column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python and I have a csv file of the following type

id, header1, header2
1, 80-80-20, a
2, 30,b
3,,c
4,60-20,d

Now I would like to separate the values in header1 to new columns where the - is the "delimiter", i.e. it should look like this:

id, header1, header2, header1_1, header1_2, header1_3
1, 80-80-20, a, 80,80,20
2, 30,b,30,,
3,,c,,,
4,60-20,d,60,20,

Any hints on how to solve this using python 3?
If you post your code, we can offer more succinct guidance. In the absence, however, I can suggest you look at the .split() function of strings. You can split the value in header1 and output it as additional columns in the outfile.
If I would have code I would have posted it but I had no idea how to even start. But I will try with the split function now.
Make an attempt, even if you can't get it to work.
We will help you with any problems, but we will not do your homework for you.