birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

What function would I use to write to a file?

Mon Jan 21, 2019 6:53 pm

I would like to keep a recording of a servo location, gps location and compass direction and save them to a file to later retrieve. Not sure where exactly to begin.

thanks

pcmanbob
Posts: 7159
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: What function would I use to write to a file?

Mon Jan 21, 2019 7:24 pm

Presuming you are talking about doing this in python ,

you would use file.write to write the data to a file of your choice , it could be a text file or a comma-separated values file depending on how you format it.

https://www.pythonforbeginners.com/file ... -in-python

lots of other examples on line.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to write to a file?

Mon Jan 21, 2019 7:53 pm

pcmanbob wrote:
Mon Jan 21, 2019 7:24 pm
Presuming you are talking about doing this in python ,

you would use file.write to write the data to a file of your choice , it could be a text file or a comma-separated values file depending on how you format it.

https://www.pythonforbeginners.com/file ... -in-python

lots of other examples on line.
thanks for the link. the way i envision the file is kind of like a database. every half second a new row would be added containing the data. I think i could do this with a data file. I want to first load all of the data in the file to memory (for faster access). i can just separate each "row" with a comma.

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to write to a file?

Mon Jan 21, 2019 7:54 pm

speaking of, how much memory do i actually have access to?

pcmanbob
Posts: 7159
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: What function would I use to write to a file?

Mon Jan 21, 2019 8:14 pm

the way i envision the file is kind of like a database. every half second a new row would be added containing the data. I think i could do this with a data file. I want to first load all of the data in the file to memory (for faster access). i can just separate each "row" with a comma.
So you are talking about a comma-separated values file ( csv ) which would give you a file like this example for my broadband speed test pi.

Image

I write to this on every test at 15 min intervals, if you wanted to keep it in memory, then I guess you could, but have never tried.
speaking of, how much memory do i actually have access to?
how much ram you have depends on the pi you are using pi2B - 3B have 1 Gb, zero or A have 512 Mb , Some of that will be used for the OS and what ever you have running on your pi.

How much storage space you have on the SD card depends on what size SD card you have and what's installed on it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

W. H. Heydt
Posts: 11019
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: What function would I use to write to a file?

Mon Jan 21, 2019 8:35 pm

birddseedd wrote:
Mon Jan 21, 2019 7:53 pm
thanks for the link. the way i envision the file is kind of like a database. every half second a new row would be added containing the data. I think i could do this with a data file. I want to first load all of the data in the file to memory (for faster access). i can just separate each "row" with a comma.
If you want to put the data into a database table (or tables), you can. You'll have to connect to the database and then construct an SQL string that inserts the data where you want it, then call the database to run the SQL statement. With compiled languages, this is done with pre- or post-processing. Fro interpreted languages, you have to import the relevant functions so you can call them.

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: What function would I use to write to a file?

Mon Jan 21, 2019 9:25 pm

Some breif datalogger techniques can be learnt here
https://projects.raspberrypi.org/en/pro ... ata-logger

And more so in the original worksheet 1 & 2
https://github.com/raspberrypilearning/ ... rksheet.md

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to write to a file?

Mon Jan 21, 2019 10:11 pm

I started playing with some csv writing. it seems mostly straight forward. but i'm runing into a roadblock. I will be taking gps-tk info which i believe will be formatted with commas. which would throw off my file format. I googled tab separated value and that seems a little more involved.

pfletch101
Posts: 536
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: What function would I use to write to a file?

Mon Jan 21, 2019 10:22 pm

birddseedd wrote:
Mon Jan 21, 2019 10:11 pm
I started playing with some csv writing. it seems mostly straight forward. but i'm runing into a roadblock. I will be taking gps-tk info which i believe will be formatted with commas. which would throw off my file format. I googled tab separated value and that seems a little more involved.
There are at least two standard workarounds for the problems of storing data in CSV format when the data includes strings which themselves contain commas. The more usual one is to quote the strings, e.g.:

Code: Select all

1,2,3,"four, five ,six"
Another approach which works is to use something other than a comma (e.g. a semi-colon) as the separator character, so that the previous entry would become:

Code: Select all

1;2;3;four, five, six

birddseedd
Posts: 43
Joined: Fri Jan 04, 2019 3:28 pm

Re: What function would I use to write to a file?

Mon Jan 21, 2019 10:32 pm

pfletch101 wrote:
Mon Jan 21, 2019 10:22 pm
birddseedd wrote:
Mon Jan 21, 2019 10:11 pm
I started playing with some csv writing. it seems mostly straight forward. but i'm runing into a roadblock. I will be taking gps-tk info which i believe will be formatted with commas. which would throw off my file format. I googled tab separated value and that seems a little more involved.
There are at least two standard workarounds for the problems of storing data in CSV format when the data includes strings which themselves contain commas. The more usual one is to quote the strings, e.g.:

Code: Select all

1,2,3,"four, five ,six"
Another approach which works is to use something other than a comma (e.g. a semi-colon) as the separator character, so that the previous entry would become:

Code: Select all

1;2;3;four, five, six

Code: Select all

import csv
import time



with open('MowingPattern', 'wt') as out_file:
    tsv_writer = csv.writer(out_file, delimiter='\t')
    tsv_writer.writerow(['servo1pos', 'servo2pos', 'rtkgpspos', 'compasspos'])
    t_end = time.time() + .5 * .5
    while time.time() < t_end:
        tsv_writer.writerow(["23", "15", "N40° 44.9064, W073° 59.0735", "N40° 44.9064, W073° 59.0735"])


Return to “Beginners”