0

var 1 is constantly changing with every new line that is written in the csv file. Any suggestion how can I get the value outside of the function. This example does not work for me. I updated my code and added second function which is exactly the same but is reading another file. Now I only get print from the first function only. If I disable the first function I can get the print from the second function. Is there a way to print both of them or maybe three or four if I add later new functions ?

import sys
import time
import datetime
import os


class Application():

    def loop_one(self):

       filename = 'Test.csv'
       mycsv = open(filename, 'r')
       mycsv.seek(0, os.SEEK_END)

       while 1:
            time.sleep(1)
            where = mycsv.tell()
            line = mycsv.readline()

       if not line:
            mycsv.seek(where)

       else:
           arr_line = line.split(',')
           var1 = arr_line[5]
           mydate = datetime.datetime.now()
           print var1, mydate.strftime("%H:%M:%S:%f")
           return var1

    def loop_two(self):

       filename2 = 'Test2.csv'
       mycsv2 = open(filename2, 'r')
       mycsv2.seek(0, os.SEEK_END)

       while 1:
            time2.sleep(1)
            where2 = mycsv2.tell()
            line2 = mycsv2.readline()

       if not line2:
            mycsv2.seek(where2)

       else:
           arr_line2 = line2.split(',')
           var2 = arr_line2[5]
           mydate2 = datetime.datetime.now()
           print var2, mydate.strftime("%H:%M:%S:%f")
           return var2

s = Application()
var1 = s.loop_one()
var2 = s.loop_two()
4
  • 1
    Have you tried saving a reference? var1 = s.loop_one() Commented Dec 1, 2016 at 23:30
  • I tried var1 = s.loop_one() and then print var1 but got nothing Commented Dec 1, 2016 at 23:33
  • Are you sure your indentation is correct? Should your if and else statement be within the while loop? Commented Dec 1, 2016 at 23:35
  • Yes that was the problem. Thanks.. Commented Dec 1, 2016 at 23:44

3 Answers 3

1

You can declare a variable inside the init function so you can use that anywhere

class A(object):
    def __init__(self):
        self.x = 'Hello'

    def method_a(self, foo):
        print self.x + ' ' + foo

In your case you can do something like this (not tested)

class Application():

    def __init__(self):

       self.filename = 'Test.csv'
       self.mycsv = open(self.filename, 'r')
       self.mycsv.seek(0, os.SEEK_END)
       self.var1 = ''

    def loop_one(self):
        while 1:
            time.sleep(1)
            where = self.mycsv.tell()
            line = self.mycsv.readline()

            if not line:
                self.mycsv.seek(where)
                # you need a break here or somewhere :D

            else:
                arr_line = line.split(',')
                self.var1 = arr_line[5]
                mydate = datetime.datetime.now()
                print self.var1, mydate.strftime("%H:%M:%S:%f")
                return self.var1

s = Application()
s.loop_one()
Sign up to request clarification or add additional context in comments.

Comments

0

You have move your if else block inside the for loop otherwise your code will stuck in infinite loop.

import sys
import time
import datetime
import os

class Application():

    def loop_one(self):

       filename = 'Test.csv'
       mycsv = open(filename, 'r')
       mycsv.seek(0, os.SEEK_END)

       while 1:
            time.sleep(1)
            where = mycsv.tell()
            line = mycsv.readline()

            if not line:
                 mycsv.seek(where)

            else:
                arr_line = line.split(',')
                var1 = arr_line[5]
                mydate = datetime.datetime.now()
                print var1, mydate.strftime("%H:%M:%S:%f")
                return var1

s = Application()
s.loop_one()

Comments

0

Accessing var1 outside the function

class Application:
    var1 = None

    def loop_one(self):
        # Code
        while True:
            # Your code
            if not line:
                # Code
            else:
                global var1
                # Code
                var1 = arr_line[5]

    # Then you can access it via
    def show():
        print(var1)

Comments

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.