-3

I have some data on a CSV file. As you can see in the code, I can read the file and print the info I need. The problem is when I try to create a new CSV file with some info of Original CSV file. I would like to save my analyzed info in a new CSV. I don't know how to use the original info to make a new file.

Data.csv enter image description here

import csv

with open('Data.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        analyzed = (row[0],row[3],row[3]<0.25)
        print(analyzed)
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
Martin Bouhier
  • 212
  • 2
  • 12
  • It sounds like you're looking for how to write data to a CSV. The fact it came from another CSV isn't too relevant here. If you look up how to write data to a CSV you should be in good shape. – TankorSmash Sep 28 '17 at 20:12
  • check this dup : https://stackoverflow.com/questions/14257373/skip-the-headers-when-editing-a-csv-file-using-python/14257599#14257599, except the skipping header part... – PRMoureu Sep 28 '17 at 20:13
  • 1
    I'd start with the [csv](https://docs.python.org/3/library/csv.html) documentation. There's lots of examples. – glibdud Sep 28 '17 at 20:13
  • Thanks @PRMoureu It was very helpful! – Martin Bouhier Sep 28 '17 at 20:17
  • @displayname, I'm not sure editing their code, even if its just to correct mispelled variable names, is the right thing to do. Not sure though. – TankorSmash Sep 28 '17 at 20:34
  • 1
    @TankorSmash Let's not forget the upper case .. I agree with you and I'm usually not doing that but in that case the worst case scenario is he, from now on, remembers how to spell it right :) – Stefan Falk Sep 28 '17 at 20:36

1 Answers1

0

You probably want to use pandas when it comes to CSV files or table-like data:

import pandas as pd

df_data = pd.DataFrame.from_csv('Data.csv')

# Analyze
for index, row in df_data.iterrows():
    pass

df_data.to_csv('new_Data.csv')

For reading you have several options like

and, as you see, use

to save your transformed or newly created DataFrame.

For installation run

pip install pandas
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378