1

I would like to add hundreds of new lines in two columns in my csv files which is presented like this (with Sublime Text):

4564,"2,82402927004573E-05"
4565,"3,43440547204476E-05"
4566,"4,04519168926086E-05"
4567,"4,65639435774783E-05"
4568,"5,26796365520546E-05"
4569,"5,87994985608482E-05"
4570,"6,49231317058272E-05"

And I would like to add lines like this:

4564,"2,82402927004573E-05"
4565,"3,43440547204476E-05"
4566,"4,04519168926086E-05"
4567,"4,65639435774783E-05"
4568,"5,26796365520546E-05"
4569,"5,87994985608482E-05"
4570,"6,49231317058272E-05"
4571,"0"
4572,"0"
4573,"0"
etc...

I have tried this code:

from csv import reader
import numpy as np
import csv
from mpdaf.obj import Spectrum, WaveCoord

wave = range(7308, 9349, 1)
with open('transmission_curve_HST_ACS_HRC.F606W.csv', 'a') as f:
     writer = csv.writer(f)
     writer.writerow(wave)

But as I've seen it adds all the data from wave on one line at the end, yet I would like to add it as one data per line in the first column.

Any idea how to do this ?

Thank you

Geo573
  • 142
  • 1
  • 10
  • Given the deleted answer offered, I'm confused what you're asking here. "add hundreds of new lines in two columns"? That can be read in multiple ways, not helped by opening the file in append mode. You need to clarify what you're trying to do. – roganjosh Oct 22 '19 at 08:19
  • Let me edit my post to show what I want :) – Geo573 Oct 22 '19 at 08:20
  • Also I was thinking of using a loop with wirterow but I'm not sur how – Geo573 Oct 22 '19 at 08:22
  • Ok, so my first reading was along the right lines (but we wouldn't have known about the `"0"`). `writer.writerows([[item, "0"] for item in wave])`? – roganjosh Oct 22 '19 at 08:23
  • This adds the datas [item, "0"] in a single row, same issue I had with my previous code :/ – Geo573 Oct 22 '19 at 08:29

1 Answers1

2

You can use pandas to achieve your goal:

df = pd.read_csv('transmission_curve_HST_ACS_HRC.F606W.csv', header=None)
wave = pd.DataFrame(list(range(7308, 9349, 1)))
wave[1] = '0'
df = df.append(wave, ignore_index=True)
df.to_csv('transmission_curve_HST_ACS_HRC.F606W.csv', header=False, index=False)
zipa
  • 27,316
  • 6
  • 40
  • 58