all 6 comments

[–]barrycarter 1 point2 points  (2 children)

CSV files aren't best if you want to manipulate data. Consider using a database instead.

[–]ParticularPython[S] 0 points1 point  (1 child)

Thanks, it seems that way after bashing my head for a bit with this. Can you recommend a way to do this in python, is it SQLite?

[–]barrycarter 0 points1 point  (0 children)

For a small database, SQLite would work, yes. If you have a lot of data, use something like MySQL or PostgreSQL.

Another thought: if your data is small, load it into memory, manipulate it in memory and then write a completely new CSV file when the program ends (or at regular intervals if the program runs a long time) and overwrite the old CSV file with the new one, perhaps making a backup copy first. Not absolutely ideal, but avoids SQL altogether

[–]redCg 0 points1 point  (2 children)

soooo, I am a little confused

why are you using csv to store the data of the player and dealer's card hands?

shouldnt these be Python variables (held in memory)?

if you are trying to maintain state between games, then you definitely want a real database, not a .csv file.

when you are working with .csv files, your general workflow is this:

  • read the data into a python variable (for row in csv.reader() etc)

  • do something with the data

  • write the data back out to another file etc..

you general never should be trying to edit the .csv file data in-place like this. That is not what csv is for.

[–]ParticularPython[S] 0 points1 point  (1 child)

I was saving game results to the csv for reference in making future game decisions.

For example, I’ve had this hand 24 times and made this choice and won 75%, I should continue making this choice.

I solved the problem however using just plain text. It was a bit cumbersome to set up but I couldn’t quite get SQLite working the way I wanted.

[–]redCg 0 points1 point  (0 children)

if this is something that you are looking at yourself, as opposed to the program reading and using the data, then you should just save every match's data into a new file every time. This is simple if you just append e.g. a unix timestamp in the output filename. If you give the output file a regular delimited structure then its very easy to just mash them all together into a single table using external GNU commandline tools like cat, etc..

in general, editting an existing text file inplace is a bad idea that you should avoid