0

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)

  
18
  • 1
    Your requirements are unclear. Could you please detail your post with examples and a clearer explanation. Commented Nov 13, 2016 at 15:52
  • Read: stackoverflow.com/help/how-to-ask Commented Nov 13, 2016 at 15:52
  • Could you post the input data that you're working with? Commented Nov 13, 2016 at 15:58
  • ss = ser.readlines(). Read the whole file in at once and then write it. Commented Nov 13, 2016 at 16:06
  • @gr1zzlybe4r the data in serial port is generated one at a time and written to serial port so it's a loop which read every new data Commented Nov 13, 2016 at 16:09

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

6 Comments

This works but this leave the first line . Any possible errors????????????
@AzeemAhamed - I added a runnable example... try it and let me know what isn't working.
This one write in the second row and this leave first row blank
@AzeemAhamed - Embarassingly, I had a bug in the code but it should not have affected the row count. I added a print (its a python 2.x style print - I assume that's what you are using) of the output data. Can you comment back with that exact data so I can see where this blank row is.
When using 'wb' instead of 'w' then working fine. Thanks a lot bruvvvvvvvv>>>>>>>>>
|
1

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()

2 Comments

I got this error "AttributeError: 'int' object has no attribute 'append' "
@AzeemAhamed Did you add the result = [] before the loop like in the code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.