0

I am trying to read an string from a text file. After I read it I want to convert it into an array or variable. In the text file I can read 'b' I sign 'b' to x, x='b' ,and I want to use how can I put 'b' as a variable b so that s = sin(2*pi*x) is s = sin(2*pi*b).

Object: I am trying to setup a configure file so that I could easily change the varibles of input from the textfile.

Final step is that I can print it out.

b = arange(0.0, 4.0, 0.01)
t = arange(0.0, 2.0, 0.01)
x = ['b'] // result after I read from the file
x = changestringtoVarible('b')

s = sin(2*pi*x)
plot(x, s)
xlabel('time(s)')
ylabel(y)
title(ttl)
grid(True)

I want to choose if I use b or t as xaxis from the text file, that is why I asked this.

2
  • post a sample of your file Commented Nov 7, 2013 at 20:52
  • @void I already updated it. Commented Nov 7, 2013 at 20:56

3 Answers 3

3

You're doing a primitive form of parsing. Your variable x will contain the character 'b' or 't', and you want use that to control how an expression is evaluated. You can't use the variable x directly in the expression, because it just contains a character. You have to execute different code depending on its value. For a simple case like this, you can just use an if construct:

...
b = arange(0.0, 4.0, 0.01)
t = arange(0.0, 2.0, 0.01)

if x == 'b':
    xvalue = b
elif x == 't'
    xvalue = t

s = sin(2*pi*xvalue)
plot(xvalue, s)
...

For a larger number of cases, you can use a dictionary:

...
xvalues = { 'b' : arange(0.0, 4.0, 0.01),
            't' : arange(0.0, 2.0, 0.01),
            # More values here
          }

xvalue = xvalues[x]
s = sin(2*pi*xvalue)
plot(xvalue, s)
....
Sign up to request clarification or add additional context in comments.

4 Comments

Ok Let me explain in this way, if there is so many varibles to choose, it doesn't make sense to use if else statement. Because I am trying to setup a configure file so that I could easily change the varibles of input from the textfile.
I am doing a big project, if the varible is store in other files, I couldn't do this, like 2000 variables.
For a such a large number, @Farhadix's answer is better.
Thx for your effort, i up your solution also!
1

You can use eval.

For example:

b='4'
formula = "2*x"
formula = formula.replace("x", "%d")
Result = eval(formula % int(b))

5 Comments

please see my upper comment...I didn't get you.
I think you can use ConfigParser
Google it. ConfigParser is so easier than you think. You have sections and variables in them. Accessing to them is as easy as defining variable.
Could you give me a link so that I could get a good one to follow :)
Hey man! I figure out the answer using the combination of yours and @crs thx u guys
1

If you just want to use a string as a variable you can use pythons exec function:

>>> exec("some_string"+"=1234")
>>> some_string
1234

update:

You better want to use another name for the x variable in s = sin(2*pi*x), we might call x y from now on.

b = arange(0.0, 4.0, 0.01)
t = arange(0.0, 2.0, 0.01)

Assumed that x defines which one should be used, you can use the following code to assign it to a variable:

exec("y"+"="+x)
s = sin(2*pi*y)

That one should work. But i would recommend you to change you code to something more solid (e.g. using a dictionary).

4 Comments

Because I am trying to setup a configure file so that I could easily change the varibles of input from the textfile. I want to do x = b, and use it as input. could you please try it on my code? I tried but it doesn't work...
Please write complete sentences.Otherwise no one knows what you are trying to tell us.
if i do exec("y"+"="+"b") it works perfectly, but I have to do exec("y"+"="+x) it doesn't work. if I do exec("y"+"="+'x') it will return y = 'b' instead of y=b.
hey, thx man! I first set up an ConfigParser file and configer it, the input is a string instead of the list of string, now I use your strategy to solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.