[Tutor] string from input file
Hansen, Mike
Mike.Hansen at atmel.com
Wed Apr 30 00:07:50 CEST 2008
> -----Original Message-----
> On
> Behalf Of Bryan Fodness
> Sent: Tuesday, April 29, 2008 3:54 PM
> To: tutorpythonmailinglist Python
> Subject: [Tutor] string from input file
>
> I am trying to get values from an input file,
>
> 0.192
> Custom
> 15
> IN
>
> but, when I check to see if they are equal it is not true.
>
> f = open(infile, 'r')
> s = f.readlines()
> f.close()
>
> Block = str(s[1])
> Angle = float(s[2])
> Position = str(s[3])
>
> if Block == 'Custom':
> print 'equal'
>
> if I print Block+'Custom', I get,
>
> Custom
> Custom
>
> when I would have expected
>
> CustomCustom
>
> Can someone help me figure out if I am not reading the
> values in correctly, is there a control character at the end?
> The float value is ok.
Your string has a line ending in it, so it doesn't match. It's
essentially "Custom\n". Just use a slice to remove the line ending
character(\n). Also, I don't think you need to convert s[1] to a
string. I think it's already a string.
Block = s[1][:-1]
In [1]: x = "Custom\n"
In [2]: y = x[:-1]
In [3]: y
Out[3]: 'Custom'
Mike
More information about the Tutor
mailing list