Trying to read from a text file to generate a graph

Steve Gronicus at SGA.Ninja
Thu Jul 29 04:11:06 EDT 2021


Thank you, the responses here have been extremely helpful.
Steve

----------------------------------------------------------------------------
----------------
Footnote:
There's 99 bugs in the code, in the code.
99 bugs in the code.
Take one down and patch it all around.
Now there's 117 bugs in the code.





-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga.ninja at python.org> On
Behalf Of Stephen Berman
Sent: Wednesday, July 28, 2021 5:36 PM
To: python-list at python.org
Subject: Re: Trying to read from a text file to generate a graph

[Resending to the list only, since I couldn't post it without subscribing.]

On Wed, 28 Jul 2021 11:58:21 -0400 "Steve" <Gronicus at SGA.Ninja> wrote:

> I forgot about the no-file rule...
>
>> On 28Jul2021 02:55, Steve <Gronicus at SGA.Ninja> wrote:
>> I am going though a struggle with this and just don't see where it 
>> fails.  I am using the Dual Bar Graph.py program from 
>> https://matplotlib.org/stable/gallery/index.html website.  The file 
>> from the web site works so that shows that all my installations are 
>> complete.
>>
>> My program, LibreGraphics 05.py program runs but the graph is all 
>> smutched up.  I am pulling data from the EXCEL-FILE.txt into the 
>> program, selecting three values for each line and creating three 
>> variables formatted as is shown in the original demo file.
>>
>> When you run the program, choose 112 when prompted. You will see the 
>> values of the variables I want to pass to the graph section of the 
>> code.  If the values are hardcoded, the graphs look good.  When the 
>> variables generated by my section of the code, it does not.

The problem is due to the values of Sensors, TestStrips and SampleNumber
being strings; what you want is for them to be lists, as in the assignments
you commented out.  And since the data from the file is read in as strings,
you have to cast the elements of the Sensors and TestStrips lists to
integers, since you want the numerical values.  The following code does the
job:

    Sensors = []
    TestStrips = []
    SampleNumber = []

    x = 1
    SensorNumber = input("Enter senaor number: ")
    with open("_EXCEL-FILE.txt", 'r') as infile:
        for lineEQN in infile:
            if (lineEQN[0:1]== "."):
                SN = lineEQN[44:48].strip()
                if (SensorNumber == SN):
                    SN = x
                    SampleNumber.append(SN)

                    sv = lineEQN[25:29].strip()
                    Sensors.append(int(sv))

                    tv = lineEQN[32:37].strip()
                    TestStrips.append(int(tv))

                    x += 1

    labels = SampleNumber

Add the rest of your code from the second half to make the desired bar
chart.

Steve Berman
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list