1

I have defined some constant in my python code. Those constants are the following :

pairs = (
 (0.0, 0.25, 0.2, 0.30),
 (0.25, 0.5, 0.35, 0.45),
 (0.5, 0.75, 0.5, 0.6),
 (0.75, 1.0, 0.65, 0.75)
) 

I want to put those constants in text file and read them from my code. How is it possible to do so? How can I read in exact the same way those constants?

EDIT: I tried to read the variables using the following code:

with open ("test.txt", "r") as myfile:
   data=myfile.readlines()

However it does not produce the desired output.

konstantin
  • 853
  • 4
  • 16
  • 50
  • 4
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Dec 20 '16 at 14:17
  • There are several ways, but if you don't know any way It looks like you should read up on [json](https://docs.python.org/2/library/json.html). There are many tutorials on this subject, if the official documentation is not clear to you. – Rory Daulton Dec 20 '16 at 14:17
  • 1
    You could consider serializing by mean of pickle: https://docs.python.org/2/library/pickle.html – jimifiki Dec 20 '16 at 14:19
  • There's also [csv](https://docs.python.org/3/library/csv.html) – Andrea Corbellini Dec 20 '16 at 14:22
  • You can directly write the Python code in a file and read it in and use the `eval` function to create a pairs variable. – rassa45 Dec 21 '16 at 01:17

2 Answers2

1
import csv
with open(path, 'rb') as f:
    reader = csv.reader(f)
    list_out = list(reader)
    print list_out

Creates the output in list format

[['0.0', ' 0.25', ' 0.2', ' 0.30'], ['0.25', ' 0.5', ' 0.35', ' 0.45'], ['0.5', ' 0.75', ' 0.5', ' 0.6'], ['0.75', ' 1.0', ' 0.65', ' 0.75']]

Input file is created and saved as follows

0.0, 0.25, 0.2, 0.30
0.25, 0.5, 0.35, 0.45
0.5, 0.75, 0.5, 0.6
0.75, 1.0, 0.65, 0.75
Shijo
  • 9,313
  • 3
  • 19
  • 31
  • You may benefit from [`QUOTE_NONNUMERIC`](https://docs.python.org/3/library/csv.html#csv.QUOTE_NONNUMERIC), so that these numbers are automatically converted to floats – Andrea Corbellini Dec 20 '16 at 14:29
0

Writing them is easy

def writeinformationtofile(info, thefilename): # writes information to file
  with open(thefilename, "w") as filea:
    filea.write(info)

Interpreting them is similarly easy. First read the file

with open(thefilename, "r") as filea:
  openedinfo = openedfile.read()

This will be a string. Then turn it into a tuple using the following as a source of information

Using python's eval() vs. ast.literal_eval()?

import ast
ast.literal_eval(openedinfo)
Community
  • 1
  • 1
A. N. Other
  • 392
  • 4
  • 14
  • 1
    `"%s" %(thefilename)` is equivalent to `thefilename`. Also, you probably wanted to use `filea.write(repr(info))`. BTW, consider using context managers (i.e. `with` blocks) when dealing with files, to ensure that they are properly closed in case an error occurs – Andrea Corbellini Dec 20 '16 at 14:23
  • Thanks for the comment - I believed I have edited as you suggested. Let me know if I could improve it further. Thank you! – A. N. Other Dec 20 '16 at 14:28