Somehow python will not let me open a file, and write an integer into this file.
My script goes something like this: -
self.l__c = 4
-
self.l__calculation = (self.status) #at this point, self.status is 16
-
if <condition>:
-
self.l__calculation = int(self.l__calculation) + (self.l__c)
-
l__write = open(self.my_file,'w').write(self.l__calculation)
-
-
-
self.l__calculation = int(self.l__calculation) + (self.l__c)
<---when printed, this line works just fine, it returns 20 (16 + 4), I then want to write 20 to a my_file, but python tells me that my first argument must be string and not int, I WANT TO WRITE ONLY AN INT TO THIS FILE, NOT STRING!
6 1983 bvdet 2,851
Expert Mod 2GB
Please use code tags when posting code!
You must convert the integer to a string to write it to a file. Please see Python documentation here.
The point of writting an int is to do calculations, I can't calculate strings.
eg on string: 16 + 4 = 164
eg on int: 16 + 4 = 20
Write it to the file as a string, when you read it back, cast it as an int()?
On the other hand if you really do want to save a data type to a file, I suggest you check the Python manual for the pickle module. Example: -
import pickle
-
-
def write(data, outfile):
-
f = open(outfile, "w+b")
-
pickle.dump(data, f)
-
f.close()
-
-
def read(filename, "r+b"):
-
f = open(filename)
-
data = pickle.load(f)
-
f.close()
-
return data
-
-
if __name__ == "__main__":
-
some_data = [123, 456, [0, 1, 2], "and a string!"]
-
write(some_data, "temp.file")
-
read_data = read("temp.file")
-
print read_data
-
Hope this helps!
bvdet 2,851
Expert Mod 2GB
Excellent post, Hackworth. Many of my applications write data to a text file, and I convert the data from string as required when the data is read. I used pickle in the past, but decided to get away from it so my data files will be readable and because the amount of data is rather small. Each value is passed to a function which will attempt to type cast it to integer, float, formatted dimension (as in 1/2 to 0.5 units), or list. If the type castings fail, the original string is returned. Therefore tuples will be returned as strings which suit my purposes. The point is, the data is written as a string and read as a string, so choose the method that best suits you to type cast your integers.
Here's my function: - def fdim(s):
-
# return a float given a fraction (EX. '1/2')
-
ss = s.split('/')
-
return float(ss[0])/float(ss[1])
-
-
def evalList(s):
-
if s.strip().startswith('[' ) and s.strip().endswith(']'):
-
try: return eval(s)
-
except: pass
-
return s
-
-
def convertType(s):
-
for func in (int, float, fdim, evalList):
-
try:
-
n = func(s)
-
return n
-
except:
-
pass
-
return s
Thats too complicated, heres how:
save the int as string (as requested) on text file
read text and save value on memory
do the calculations (value on memory + int)
save this result as value on memory
write value on memory to text as string.
And Vwala! the calculation is done.
Thanks for the reply's
bvdet 2,851
Expert Mod 2GB
eksaII - By golly, I think you have it!
BV
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Phil Powell |
last post by:
<?php
class FileRemoval {
var $fileNameArray, $isRemoved, $errorMsg = '';
function FileRemoval() {
$this->fileNameArray = array();
$this->isRemoved = 0;
}
|
by: gruddo |
last post by:
Hi this is my first post.
I have two webservers. I can run php code fine on one but not the
other. Both webservers can read files only one can write. I have looked
around at other posts and they...
|
by: Viviana Vc |
last post by:
How can I programatically do the equivalent of the following:
cacls "C:\Program Files\test" /T /G Everyone:f ?
Thanks,
Viv
|
by: Dorwin C. Black |
last post by:
Do any gurus out there know what the source code for the STL binary
search algorithm "lower_bound" would look like? I want to make a slight
modification called "lower_bound_if" which would take a...
|
by: ChrisC |
last post by:
if I had outfile.write((char*) record, sizeof( record ))
how would I get write to return a newline?
as it is it doesn't.
--
ChrisC - chrispche@nospam.yahoo.co.uk
|
by: Antony Sequeira |
last post by:
Hi
While looking at some code I realized that the built in
setTimeout
function takes a string that is later
evaluated in the original caller's context.
How does one achieve something similar in...
|
by: Stumped and Confused |
last post by:
Hello,
I really, really, need some help here - I've spent hours trying to find a
solution.
In a nutshell, I'm trying to have a user input a value in form's
textfield. The value should then be...
|
by: td0g03 |
last post by:
Here is the question....Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two...
|
by: Leo Jay |
last post by:
I'd like to read and write the same socket in different threads.
one thread is only used to read from the socket, and the other is only
used to write to the socket.
But I always get a 10022...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |