0

I'm receiving data from an Arduino each second but I can't save it, it writes the continous data in the same cell on the csv file and just is changed it each time that the new value is getted. I'm triying with the newline='' and the write row but is not working.

valueenc = (value)
                print("Valueencoded", valueenc)
                #print("Message received: "  + valueenc)
                f = open(f'{root_path}/Desktop/microphone_dump.csv','w+', newline ='')
                #f.write(valueenc)
                with f:
                    write = csv.writer(f) 
                    write.writerows(valueenc)
2
  • 1
    maybe open file only once - at start - and don't close it. O maybe use mode a for append. I'm not use how works mode w+ but mode w deletes previous content. Commented Jul 18, 2021 at 2:12
  • 1
    what do you have in valueenc ? writerows expects list with rows (which are lists too) like [ [ "row1-col1" , "row1-col2"], [ "row2-col1" , "row2-col2"] ] Commented Jul 18, 2021 at 2:14

2 Answers 2

0

Problem can make mode w+ (which deletes all data when you open file) and with f: which closes file.

I see two possible solutions:

First: open file only once - at start - and close it only once - at the end.

# at start
f = open(f'{root_path}/Desktop/microphone_dump.csv', 'w+')

# ... code ...

valueenc = value
print("Valueencoded", valueenc)
write = csv.writer(f) 
write.writerows(valueenc)

# ... code ...

# at the end
f.close()

But it may lost data when program get error.

Second: use mode a to append data

# ... code ...

valueenc = value
print("Valueencoded", valueenc)

with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
    write = csv.writer(f) 
    write.writerows(valueenc)

# ... code ...

And you can use with to close file directly after writing.


EDIT:

If you want to write only one row with two values then you should use writerow without char s in name.

with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
    write = csv.writer(f) 
    write.writerow( [value1, value2] )  # without `s` in name `writerow`

And if you would have many rows then you could use writerows

rows = [
   [value1, value2],   # row 1
   [value3, value4],   # row 2
]

with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
    write = csv.writer(f) 
    write.writerows( rows )  # with `s` in name `writerows`

BTW:

Even for single value in row you have to use list(s)

    write.writerow( [value1] )  # without `s` in name 

and

rows = [
   [value1],   # row 1
   [value2],   # row 2
]

    write.writerows( rows )  # with `s` in name `writerows`
Sign up to request clarification or add additional context in comments.

3 Comments

now I have value1 and value 2 and I want to add in diferent cells but I'm trying with this code line: file.write(value1, value2 + "\\n") #write data with a newline but it don't work. help
if you have only one row then you need write.writerow( [value1, value2] ). it has to be writerow without char s in name. And standard write expects only one argument - one string - text = str(value1) + "," + str(value2) + "\n" and later file.write( text ). But it is safer to use module csv for this.
I added info about writing many rows and about writing single element in row(s)
0

this is the way:

file = open(f'{root_path}/Desktop/test.csv', "a")
                print("Created file")
                file = open(f'{root_path}/Desktop/test.csv', "a") #append the data to the file
                file.write(valueenc + "\n") #write data with a newline
                #close out the file
                file.close()

Comments

Your Answer

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