-2

i have a python script that collects data from a server in the form of

<hh-mm-ss>,<ddd>

here, the first field is Date and the second field is an integer digit. this data is being written into a file.

i have another thread running which is plotting a live graph from the file which i mentioned in the above paragraph.

so this file has data like,

<hh-mm-ss>,<ddd>
<hh-mm-ss>,<ddd>
<hh-mm-ss>,<ddd>
<hh-mm-ss>,<ddd>

Now i want to plot a time series Matplotlib graph with the above shown data. but when i try , it throws an error saying,

ValueError: invalid literal for int() with base 10: '15:53:09'

when i have normal data like shown below, things are fine

<ddd>,<ddd>
<ddd>,<ddd>
<ddd>,<ddd>
<ddd>,<ddd>

UPDATE my code that generates graph from the file i have described above is shown below,

def animate(i):

    pullData = open("sampleText.txt","r").read()
    dataArray = pullData.split('\n')
    xar = []
    yar = []
    for eachLine in dataArray:
        if len(eachLine)>1:
            x,y = eachLine.split(',')
            xar.append(int(x))
            yar.append(int(y))
    ax1.clear()
    ax1.plot(xar,yar)

UPDATED CODE

def animate(i):
    print("inside animate")
    pullData = open("sampleText.txt","r").read()
    dataArray = pullData.split('\n')
    xar = []
    yar = []
    for eachLine in dataArray:
        if len(eachLine)>1:
            x,y = eachLine.split(',')
            timeX=datetime.strptime(x, "%H:%M:%S")
            xar.append(timeX.strftime("%H:%M:%S"))
            yar.append(float(y))
    ax1.clear()
    ax1.plot(xar,yar)

Now i am getting the error at this line (ax1.plot(xar,yar)) how will i get over this?

Vasanth Nag K V
  • 4,860
  • 5
  • 24
  • 48

2 Answers2

0

You are trying to parse an integer from a string representing a timestamp. Of course it fails.

In order to be able to use the timestamps in a plot, you need to parse them to the proper type, e.g., datetime.time or datetime.datetime. You can use datetime.datetime.strptime(), dateutil.parser.parse() or maybe also time.strptime() for this.

Plotting the data is straight-forward, then. Have a look at the interactive plotting mode: matplotlib.pyplot.ion().

For reference/further reading:


Based on your code I have created an example. I have inlined some notes as to why I think it's better to do it this way.

# use with-statement to make sure the file is eventually closed
with open("sampleText.txt") as f:
    data = []
    # iterate the file using the file object's iterator interface
    for line in f:
        try:
            t, f = line.split(",")
            # parse timestamp and number and append it to data list
            data.append((datetime.strptime(t, "%H:%M:%S"), float(f)))
        except ValueError:
            # something went wrong: inspect later and continue for now
            print "failed to parse line:", line
# split columns to separate variables
x,y = zip(*data)
# plot
plt.plot(x,y)
plt.show()
plt.close()

For further reading:

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93953/discussion-between-moooeeeep-and-vasanth-nag-k-v). – moooeeeep Nov 02 '15 at 08:45
-1

The error tells you the cause of the problem: You're trying to convert a string, such as '15:53:09', into an integer. This string is not a valid number.

Instead, you should either look into using a datetime object from the datetime module to work with date/time things or at least splitting the string into fields using ':' as the delimiter and the using each field separately.

Consider this brief demo:

>>> time = '15:53:09'
>>> time.split(':')
['15', '53', '09']
>>> [int(v) for v in time.split(':')]
[15, 53, 9]
>>> int(time)  # expect exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '15:53:09'
>>>
code_dredd
  • 5,915
  • 1
  • 25
  • 53