saving data to txt file using python

2024/9/20 9:53:11

I am new in python, and I really need some help. I am doing this memory game where I need to save user, game score and time into a text file using python. I have tried several ways to do it, but nothing seems to work. I need to get the text what is shown after game on html page (statsParagraph) into *txt file using python In my html code I have this:

<form  action="http://....python file addres" method="GET">
<p id="statsParagraph" name="user" value=""></p>
</form>

in my javascript code I have this:

function showstatistic(){ 
var user = prompt ("What is your name?","");
alert (user + ". game is over!")
var s="";
for(var i=0;i<statistic.length;i++){var t=statistic[i].time;var timeString= (t-(t%60))/60+":"+(t%60);s += "User: " + user +" <br/>"+ " Game #"+ (i+1) +"  "+" <br/>"+" Guessed: "+ statistic[i].guessed+" <br/>"+" Minus points: "+ statistic[i].minuses+" <br/>"+" Guessed right: "+ statistic[i].plusPoints+" <br/>"+" Time: "+ timeString+" <br/>"+" <br/>";}   
$("#statsParagraph").html(s);
}   
Answer

You would open the code with

from sys import argvscript, filename = argvtarget = open(filename, 'w')

then assign each piece of info to a variable with an appropriate name

time = #x
score = #x
user = #name

then type the following

target.write(time)
target.write("\n")
target.write(score)
target.write("\n")
target.write(user)
target.write("\n")target.close()

when you run the file you would run it with an argument, that argument being the file where you would like to write the info.

>python mymemorygame.py savegamefile.txt 

You may also want to consider truncating the file, before writing to it depending on how you are saving the info (multiple files or one file)

I hope this helps, apologies if it doesn't I may have misunderstood what it is you are looking to do.

alternatively, it may be harder to write but easier to manage/implement, you could put the info in to a list that you store in a single file, rather than writing new .txt files all the time.

https://en.xdnf.cn/q/119374.html

Related Q&A

How can I create bounding boxes/contour around the outer object only - Python OpenCV

So Ive been trying to make bounding boxes around a couple of fruits that I made in paint. Im a total beginner to opencv so I watched a couple tutorials and the code that I typed made, makes contours ar…

resuming download file ftp python3.*

There is a file (1-7Gb) that you need to pick up. The network periodically falls, so it is necessary to implement the method of resume. For example, in 1 communication session downloaded 20% the networ…

printing files based on character

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected t…

Parsing CSV file using Panda

I have been using matplotlib for quite some time now and it is great however, I want to switch to panda and my first attempt at it didnt go so well.My data set looks like this:sam,123,184,2.6,543 winte…

Getting division by zero error with Python and OpenCV

I am using this code to remove the lines from the following image:I dont know the reason, but it gives me as output ZeroDivisionError: division by zero error on line 34 - x0, x1, y0, y1 = (0, im_wb.sha…

Pandas complex calculation based on other columns

I have successfully created new columns based on arithmetic for other columns but now I have a more challenging need to first select elements based on matches of multiple columns then perform math and …

how to generate word from a to z [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to webscrape all shoes on nike page using python

I am trying to webscrape all the shoes on https://www.nike.com/w/mens-shoes-nik1zy7ok. How do I scrape all the shoes including the shoes that load as you scroll down the page? The exact information I …

Pyo in Python: name Server not defined

I recently installed Pyo, and I entered Python 3.6 and typedfrom pyo import * s = Server().boot() s.start() sf = SfPlayer("C:\Users\myname\Downloads\wot.mp3", speed=1, loop=True).out()but I …

Limited digits with str.format(), and then only when they matter

If were printing a dollar amount, we usually want to always display two decimal digits.cost1, cost2 = 123.456890123456789, 357.000 print {c1:.2f} {c2:.2f}.format(c1=cost1, c2=cost2)shows123.46 357.00…