2

I have a python script that is as follows:

import argparse

if __name__ == "__main__":
   parser = argparse.ArgumentParser(description="This program parses CSV file for Agris Input from the files that are recieved from BOA",
                                epilog="*** Important not, leave the .csv off of the second argument so it does not end up in the file name.**")
   parser.add_argument('Input', help="Enter the name of the CSV file no extension.")
   parser.add_argument('Output', help="This is the output file that will be split into multiple files in the formation <outfile>_dataset_{number}.csv \nLeave the .csv extension off.")

  args = parser.parse_args()


import pandas as pd
import os
import sys

fileName = args.Input + ".csv"
outfile = args.Output

# Read data from the input file
data = pd.read_csv(fileName, sep='|', header=0)

if os.path.exists(outfile+".csv"):
   append_write = 'a' #appends if exists
   with open(output + group in data.groupyb('Number1')+'.csv', append_write) as f:
      group.to_csv(f, header=false, index=False, sep='|', float_format='%.2f')
else:
   append_write = 'w' #make a new file if it does not exist
   for Number1, group in data.groupby('Number1'):
       group.to_csv(f'{outfile}_{Number1}.csv', header=True, index=False, sep='|', float_format='%.2f')

I'm wanting if the file exist that is created from this append the data to that file, if it's not there then create a new file.

so example would be

python inOut.py book1 abc     #book1 is a csv | delimited file breaking on column called Number1

so output would create abc_100.csv then when you run the inOut.py book2 abc, it will look to see if that abc_100.csv file exists and if it does append the data.

The code above will overwrite the data and not append to it.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
Sc-python-leaner
  • 259
  • 1
  • 2
  • 13
  • Possible duplicate of [append new row to old csv file python](https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python) – rpanai Dec 12 '18 at 14:29
  • don't you need `mode = 'a'` also in your `.to_csv` argument? `with open(output + group in data.groupyb('Number1')+'.csv', mode=append_write) as f group.to_csv(f, mode=append_write, header=false, index=False, sep='|', float_format='%.2f')` – chitown88 Dec 12 '18 at 14:30
  • There is a typo in the code `.groupyb`. It is not the cause of the issue you are having, but it would be better to fix it anyway. – datapug Dec 12 '18 at 14:33
  • What is the notation `group in data.groupby('Number1')` on the `with` line supposed to mean? It checks for the presence of an (undefined!) variable `group` in the iterable `data.groupby('Number1')`. Probably not what you meant. – Martin Frodl Dec 12 '18 at 14:35
  • The group by data.groupby('Number1') is suppose to see if say abc_100.csv exists and if it does append to the file. – Sc-python-leaner Dec 12 '18 at 14:47
  • Where does this number 100 in `abc_100.csv` come from? Is it one of the values in the `Number1` column of `data`? What if the column contains other values too, such as 200, 300? Do you want to append to files `abc_200.csv`, `abc_300.csv` too? – Martin Frodl Dec 12 '18 at 15:45
  • Also, just to be clear: if file `abc.csv` exists, you want to append to file `abc_100.csv` (which may or may not exist). Correct? – Martin Frodl Dec 12 '18 at 15:49

0 Answers0