Page 1 of 1

Read data from a csv file

Posted: Fri May 05, 2017 9:51 pm
by RDS
I have successfully created many .csv files and been able to load them into a spreadsheet.
I have now created another .csv file that contains just one number.

I just need to be able to read the number back in another program and place it in the variable 'detect'.
I have tried using the following code (and many variations on it), without success.

Code: Select all

detect =0
file = open('my_file.csv', 'r')
detect = file.read ()
file.close()

print ('detect')
Could someone please advise where I am going wrong.
Thanks.

Re: Read data from a csv file

Posted: Fri May 05, 2017 10:16 pm
by scotty101
Very simple fix. Was it outputting the word 'detect' rather than the number?

Code: Select all

detect =0
file = open('my_file.csv', 'r')
detect = file.read ()
file.close()

print (detect)

Re: Read data from a csv file

Posted: Fri May 05, 2017 10:21 pm
by RDS
No, it didn't output anything at all.

Actually, I realised that it would not let me close down a window, so decided to reboot the Pi completely and it now (as you predicted, prints detect).

I have changed the program as you suggested and all is well.
Thank you.