0

I have a file with several variables that looks like this:

BTC = 375.23
SOL = 200.42
LTC = 208.91
DOT = 60.12

And a main script. How can I append those values ? Example: change BTC = 375.23 to BTC = 420.32. The methods I'm using are:

from varfile import * # imported this way for trial and error
from varfile import BTC # imported this way for trial and error
import varfile

def blah():
    varfile.BTC.append(float(420.32))
blah()

But I'm getting an error: AttributeError: 'float' object has no attribute 'append' I feel like I just need to convert the float to str somehow but not having luck. I've found a couple similar posts but nothing quite the same and nothing that works the way I'd like.

Let me clarify a little and maybe add some context. There is 2 files, one is my main script and the other file contains data - the variables I mentioned that are in varfile.py. The data file doesn't need to be any specific type of file, it can be a txt file or json, anything. When the main script is ran, I want it to read the values to the variables BTC, SOL and so on. After those values are read and used in the function, I want to change those values for next time I run the main script.

WaXxX333
  • 388
  • 1
  • 2
  • 11
  • 1
    Assuming that ```varfile``` contains ```BTC, etc..```, ```varfile.BTC``` is a float so ```varfile.BTC.append(...)``` makes no sense. What exactly are you trying to do? What do you mean by 'append those values"? What do you want to append it to? – ewokx May 10 '22 at 03:40
  • 4
    Don't save data that can change in a python file. Save it in a text or csv or json file and then parse that file – Pranav Hosangadi May 10 '22 at 03:41
  • 1
    Does this answer your question? [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) .. additionally, if you have some structured data, as @PranavHosangadi suggests, writing to some structured file or even a database will give you much better results and reliability – ti7 May 10 '22 at 03:41
  • No. It is a separate file. Take a look at the original value of `BTC`, it's `375.23`. I want to change that value. – WaXxX333 May 10 '22 at 03:43
  • Like I said, there's a main script and a file with the variables. I want to change just the values in the file `varfile.py` – WaXxX333 May 10 '22 at 03:45
  • You can change it by simply setting the variable, but it won't persist so after you restart the program you'll have the old values back – Pranav Hosangadi May 10 '22 at 03:48
  • Couldn't you just simply save it as a JSON structured file? – ewokx May 10 '22 at 03:50
  • I know how to do that, but it's not what I'm looking for. Essentially I want to read that value every time I run the script, and after it's read I want it changed for the next time I run the script. – WaXxX333 May 10 '22 at 03:50
  • JSON or not, how would I change it every time it's ran so the new value can be read the next time ? – WaXxX333 May 10 '22 at 03:51
  • Read the file. Change the value of the parsed object. Write the parsed object to file. Search how to do each of these steps, there are plenty of resources on the Internet and answers on Stack Overflow. – Pranav Hosangadi May 10 '22 at 04:01

1 Answers1

3

The reason you are getting the error is because varfile.BTC is a float.

import varfile

def blah():
    # varfile.BTC = 345.23 which is a float and has no append method
    varfile.BTC.append(float(420.32))  
blah()

If your goal is to simply change the value of the variable at runtime, This will suffice.

varfile.BTC = 0.1     # or some other value

If your goal is to change the actual contents of the file, then it shouldn't be saved in a .py file, and shouldn't be imported into your script.

If you you wanted to change the value in a plain text file it would look something like this.

BTC = 10 # this is the value you want to change it to
varfile = 'varfile.txt'

with open(varfile, 'rt') as txtfile:
    contents = txtfile.read().split('\n')
    #  contents.append("BTC = " + str(BTC))   # this line is if you just wanted to add a value to the end of the file.
    for i,line in enumerate(contents):
        if 'BTC' in line:
            contents[i] = 'BTC = ' + str(BTC)
            break

with open(varfile, "wt") as wfile:
    wfile.write('\n'.join(contents))
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • How do I append the value with `write` ? It doesn't have to be `varfile.py`, it can be `varfile.txt`. I just figured by importing it I would be able to `append` it like that, but apparently not. – WaXxX333 May 10 '22 at 03:49
  • 1
    @WaXxX333 you can't. Appending means adding onto something, and not deleting. Appendage will never read or delete anything, as it just adds onto the end. Appending `'a'` to `'abc'` results in `'abca'` – Freddy Mcloughlan May 10 '22 at 04:01
  • After playing with this for a minute I have it pretty close to what I was looking for. I think I'll be able to get exactly what I wanted out of it. Thank you. I didn't know I could ***edit*** lines like that. – WaXxX333 May 10 '22 at 04:02