Azeem Ahamed
Azeem Ahamed

Reputation: 37

Writing Data to csv file to a single row

I need to write data to a single row and column by where data is coming from the serial port. So I used to read and write row by row. But the requirement is I need to write data to the next column of a single row In PYTHON. I need to have filed at the end like this

1,2,33,43343,4555,344323

In such a way all data is to be in a single row and multiple columns, and not in one column and multiple rows.

So this writes data in one after one row.

1

12

2222

3234

1233

131

but I want 1 , 12 , 2222 , 3234 , 1233 , 131 like every single row and multiple columns.

import serial
import time
import csv
ser = serial.Serial('COM29', 57600)

timeout = time.time() + 60/6   # 5 minutes from now

while True:
    test = 0
    if test == 5 or time.time() > timeout:
        break
    ss=ser.readline()
    print ss
    s=ss.replace("\n","")
    with open('C:\Users\Ivory Power\Desktop\EEG_Data\Othr_eeg\egg31.csv', 'ab') as csvfile:
        spamwriter = csv.writer(csvfile,delimiter=',', lineterminator='\n')
        spamwriter.writerow([ s ])
        csvfile.close()
    time.sleep(0.02)

  

Upvotes: 0

Views: 7687

Answers (2)

tdelaney
tdelaney

Reputation: 77337

The csv module writes rows - every time you call writerow a newline is written and a new row is started. So, you can't call it multiple times and expect to get columns. You can, however, collect the data into a list and then write that list when you are done. The csv module is overkill for this.

import serial
import time
import csv
ser = serial.Serial('COM29', 57600)

timeout = time.time() + 60/6   # 5 minutes from now

data = []
for _ in range(5):
    data.append(ser.readline().strip())
    time.sleep(0.02)

with open('C:\Users\Ivory Power\Desktop\EEG_Data\Othr_eeg\egg31.csv', 'ab') as csvfile:
    spamwriter = csv.writer(csvfile,delimiter=',', lineterminator='\n')
    spamwriter.writerow(data)

    # csv is overkill here unless the data itself contains commas
    # that need to be escaped. You could do this instead.
    # csvfile.write(','.join(data) + '\n')

UPDATE

One of the tricks to question writing here to to supply a short, runnable example of the problem. That way, everybody runs the same thing and you can talk about whats wrong in terms of the code and output everyone can play with.

Here is the program updated with mock data. I changed the open to "wb" so that the file is deleted if it already exists when the program runs. Run it and let me know how its results different from what you want.

import csv
import time

filename = 'deleteme.csv'

test_row = '1,2,33,43343,4555,344323'
test_data = test_row.split(',')

data = []
for _ in range(6):
    data.append(test_data.pop(0).strip())
    time.sleep(0.02)

with open(filename, 'wb') as csvfile:
    spamwriter = csv.writer(csvfile,delimiter=',', lineterminator='\n')
    spamwriter.writerow(data)

print repr(open(filename).read())
assert open(filename).read().strip() == test_row, 'got one row'

Upvotes: 2

Ébe Isaac
Ébe Isaac

Reputation: 12321

Assuming your serial port data wouldn't overrun your main memory heap, the following would be the code that would suit your need.

import serial
import time
import csv
ser = serial.Serial('COM29', 57600)

timeout = time.time() + 60/6   # 5 minutes from now

result = []
while True:
    test = 0
    if test == 5 or time.time() > timeout:
        break
    ss=ser.readline()
    print(ss)
    s=ss.replace("\n","")
    result.append(s)
    time.sleep(0.02)

with open('C:\Users\Ivory Power\Desktop\EEG_Data\Othr_eeg\egg31.csv', 'w') as csvfile:
    spamwriter = csv.writer(csvfile,delimiter=',', lineterminator='\n')
    spamwriter.writerow([result])
    csvfile.close()

Upvotes: 1

Related Questions