0

I am a complete newb at this and I have the following script.. It writes some random data to .csv. My end goal is to keep this preexisting .csv but add ONE random generated datapoint to the beginning of this csv in a separate Python script.

Completely new at this -- not sure how to go about doing this. Thanks for your help.

    output = [a,b]

    d = csv.writer(csvfile, delimiter=',', quotechar='|', 

    quoting=csv.QUOTE_MINIMAL)  
    d.writerow(output)
rita
  • 101
  • 1
  • 9
  • a side note you can change your constraint function to `max(min(value,max_value),min_value)` also you cannot do what you want you will need to read in the whole csv, add your value and write out your whole csv – Joran Beasley Dec 02 '13 at 16:22
  • 1
    Take a look at [this](http://stackoverflow.com/questions/5914627/append-line-to-beginning-of-a-file) answer – RickyA Dec 02 '13 at 16:35

1 Answers1

1

Are you sure you are trying to add it to the start of the file? I feel like you would want to add it to the end or if you did want to add it at the beginning you would at least want to put it after the header row which is ['name', 'value'].

As it stands your current script has several errors when I try to compile it myself so I can help you out a bit there.

  1. The directory string doesn't work because of the slashes. It will work if you add an r in front (for raw string) like so r'C:/Users/AMB/Documents/Aptana Studio 3 Workspace/RAVE/RAVE/resources/csv/temperature.csv'
  2. You don't need JSON to import json or logging if this is the entirety of your code.
  3. Inside of your for loop you redefine the temperature writer which is unnecessary, your definition at the start is good enough.
  4. You have an extra comma in your the line output = [timeperiod, temp,]

Moving on to a script that inserts a single data point. This script reads in your existing file. Inserts a new line (you would use random values, I used 1 for time and 2 for value) on the second line which is beneath the header. Let me know if this isn't what you are looking for.

directory = r"C:/Users/AMB/Documents/Aptana Studio 3 Workspace/RAVE/RAVE/resources/csv/temperature.csv"
with open(directory, 'r') as csvfile:
     s = csvfile.readlines()
time = 1
value = 2
s.insert(2, '%(time)d,%(value)d\n\n' % \
    {'time': time, "value": value})
with open(directory, 'w') as csvfile:
     csvfile.writelines(s)

This next section is in response to your more detailed question in the comments:

import csv
import random

directory = r"C:\Users\snorwood\Desktop\temperature.csv"

# Open the file
with open(directory, 'r') as csvfile:
    s = csvfile.readlines()

# This array will store your data
data = []

# This for loop converts the data read from the text file into integers values in your data set
for i, point in enumerate(s[1:]):
    seperatedPoint = point.strip("\n").split(",")
    if len(seperatedPoint) == 2:
        data.append([int(dataPoint) for dataPoint in seperatedPoint])

# Loop through your animation numberOfLoops times
numberOfLoops = 100
for i in range(numberOfLoops):
    if len(data) == 0:
        break

    del data[0] # Deletes the first data point
    newTime = data[len(data) - 1][0] + 1 # An int that is one higher than the current last time value
    newRandomValue = 2
    data.append([newTime, newRandomValue]) # Adds the new data point to the end of the array

    # Insert your drawing code here

# Write the data back into the text file
with open(directory, 'w') as csvfile: #opens the file for writing
    temperature = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)  # The object that knows how to write to files
    temperature.writerow(["name", "values"]) # Write the header row
    for point in data: # Loop through the points stored in data
        temperature.writerow(point) # Write current point in set
Steven
  • 240
  • 1
  • 7
  • Having the file open twice with different modes will probably cause problems. You should use [`with`](http://docs.python.org/3.3/whatsnew/2.6.html#pep-343-the-with-statement) to ensure that files are closed properly, as in `with open(directory,'r') as csvfile: ...` then `with open(directory,'w') as csvfile: ...` – darthbith Dec 02 '13 at 16:49
  • I wasn't really trying to write safe code, especially since I haven't used python syntax in a while, but thank you all the same. I will edit that. – Steven Dec 02 '13 at 16:52
  • csv files should be read in binary mode, so the `mode` on the first `open()` call should be `'rb'`. To insert a new second line needs to use `s.insert(1, ...)`. That same line has an extraneous backslash character after the `%` operator. – martineau Dec 02 '13 at 18:00
  • I have tried to run your script but I am receiving the following error: s.insert(2, '%(time)d,%(value)d\n\n' % \ {'time': time, "value": value}) ^ SyntaxError: unexpected character after line continuation character – rita Dec 02 '13 at 18:03
  • Oh sorry. That was my fault I accidentally removed the new line after the \. I will change it in the answer. – Steven Dec 02 '13 at 18:06
  • 1
    If it ends up working make sure to mark this as the answer so people know the problem is closed. – Steven Dec 02 '13 at 18:14
  • Thanks Steven. I have a follow up question - should I post it here or create a new one? – rita Dec 02 '13 at 19:52
  • Umm its up to you. I'm willing to answer it here if you like I just won't be able to format it as nicely. – Steven Dec 02 '13 at 19:53
  • Your answer is still inserting a row _following_ the second line in the file, not right after the header in the first line. – martineau Dec 03 '13 at 00:32
  • Have you run the program yourself? You will see that there is an empty line between every row in the csv. I was just keeping with the format. – Steven Dec 03 '13 at 00:37
  • Ok so what I'm trying to do is render a line using this data and animate said data. So I actually need to remove the first point, unfortunately. I've tried doing s.remove(1) but this is not working unfortunately. The workflow is supposed to go like so: 1. Generate data to csv (done, see my first post) 2. Render data to graph (done) At this point all my data is tossed, so what I'm trying to get around is keeping the original data I generated, but: 3. Removing the first row 4. Generate a new point at the end 5. Re-render the data 6. Repeat – rita Dec 03 '13 at 04:05
  • However I want to keep the same number of data points.. Ideally this would cause my line to shift to the left and thus animate it. The issue comes with preserving the prior generated data -- this is what I need to keep, aside from the first and last data points. – rita Dec 03 '13 at 04:07
  • I updated the answer with a section that for your latest question. – Steven Dec 03 '13 at 13:33
  • Sorry to keep bothering you -- I've run this code and it doesn't appear to be doing anything to my data -- did it work on your end? – rita Dec 03 '13 at 15:05
  • I didn't write it back to the text file. The variable "data" is an array that contains your points within the program, you do not need to store anything in the text file unless you want to continue where you left off later. Either way I will update the code with a write to file so you know how to do it. – Steven Dec 03 '13 at 15:09
  • Is it possible to update the last column field to be the prior column field + 1? The new time value (second column) can be anything at all but the first column field I'm trying to increment by 1 each iteration. – rita Dec 03 '13 at 18:08
  • Why yes it is. I will update that soon. One thing I think would benefit you is to start reading through my code carefully, especially the comments since they will give you an idea of what does what. You can also look up any confusing syntax online. I just feel like this could be a good learning experience for you. – Steven Dec 03 '13 at 18:42
  • Thanks Steven. I have actually been doing that earlier today as there were definitely a few syntax I was confused about (i.e. the point.strip method). This is absolutely helping me learn so thank you. – rita Dec 03 '13 at 18:45
  • That is exactly what I want to hear. As long as you are gaining from it I am happy to keep helping. Anyway I updated the code so you can try that now. – Steven Dec 03 '13 at 18:55