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.

5
  • 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 and How to Ask. Commented Dec 20, 2016 at 14:17
  • There are several ways, but if you don't know any way It looks like you should read up on json. There are many tutorials on this subject, if the official documentation is not clear to you. Commented Dec 20, 2016 at 14:17
  • 1
    You could consider serializing by mean of pickle: docs.python.org/2/library/pickle.html Commented Dec 20, 2016 at 14:19
  • There's also csv Commented Dec 20, 2016 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. Commented Dec 21, 2016 at 1:17

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

You may benefit from QUOTE_NONNUMERIC, so that these numbers are automatically converted to floats
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)

2 Comments

"%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
Thanks for the comment - I believed I have edited as you suggested. Let me know if I could improve it further. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.