By Kiki via Discussions on Python.org at 26Jun2022 20:30:
I tried and it works thanks you but I would like to store values inside
another text file ? Do I have to use fichier.write to do that ?
If you mean file.write, that will be inherently involved at some
point. But you don’t need to use it directly.
A common format for small amounts of data is the CSV file, and Python
comes with a csv module for writing to these files:
Near the top of that page is documentation of the csv.writer function
which has a simple example of writing some data to a .csv file.
General sketch:
data = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
]
with open('some_filename.csv', 'w') as f:
with csv.writer(f) as csvw:
for data_row in data:
csvw.writerow(data_row)
Obviously data would in fact be the data you’ve gathered in your
programme. So the process is:
open a file for write
get a CSV writer which uses that file
call the .writerow method of that writer with each row of data in
turn
This is why I offered the basic approach above. You may find the basic python fits your programming level better than importing a package (or even importing a module) and learning how to use it—while you’re also learning Python.
A very important principle is:
It’s VERY good to understand the code you’re working with so that you can change it successfully. You also need to know how it works so that you can see why it doesn’t work when it fails or breaks.
Attention, csv.writer() does not return a context manager. In fact it has nothing to clean-up. Fixed code:
import csv
data = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
]
with open('some_filename.csv', 'w') as f:
csvw = csv.writer(f)
for data_row in data:
csvw.writerow(data_row)
I agree, but… a beginner cannot understand all the details or even they will never be important for him.[1] I think that for some people it is fine to use library functions and treat them as black boxes without knowing how they work inside.[2]
I think this particular task is too complicated for the level Kiki is at. There are learning platforms with much simpler tasks:
I fully agree, and often dig into a module’s internal code when the documentation isn’t clear enough. [1] In the post above I’m just talking about the extra work of learning what arguments the module’s functions have, its methods, etc. Gearheads like the regulars here that like to browse the module’s internal code to really understand how it all works—and that’s why I enjoy spending time here so much. Still, it’s good to be careful about tossing someone the keys to a Ferrari or a Porsche when they’re still learning to drive and understand the road signs.
Imports also open the door to feeling like you should explore the rest of the functions in the module, for better or for worse… When someone is learning Python, they should probably stay with the built-in functions that don’t require imports until they know more than half of the rudimentary functions.
I’d be interested to hear what @TazFleck (Kiki) has to say about using built-in core functions before extending to modules. If you don’t need the execution speed of a function written in C and aren’t working in a production environment, the slowness isn’t a bad thing.
Like with OpenCV. Many of the docs are automatically generated and are sometimes MORE cryptic and often far less complete than the source code and comments. ↩︎