RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Read data from a csv file

Fri May 05, 2017 9:51 pm

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.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Read data from a csv file

Fri May 05, 2017 10:16 pm

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)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Read data from a csv file

Fri May 05, 2017 10:21 pm

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.

Return to “Python”