Changes between Version 51 and Version 52 of en/ProgrammingRobotsCourse/GettingWwwInfo


Ignore:
Timestamp:
May 21, 2024, 3:25:02 PM (2 months ago)
Author:
Ales Horak
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • en/ProgrammingRobotsCourse/GettingWwwInfo

    v51 v52  
    6868            sys.path.remove(self.folderName)
    6969        self.onStopped() #activate the output of the box
     70}}}
     71
     72=== Load local data
     73
     74The same approach may be used for accessing your own data (e.g. in `csv` or `json` format) stored within your application directory. You need to include the file within the app package description `....pml` file as:
     75{{{#!xml
     76<Resources>
     77    <File name="data.csv" src="data" />
     78</Resources>
     79}}}
     80and load it in a Python box with correct path
     81{{{#!python
     82import csv
     83
     84class MyClass(GeneratedClass):
     85    def __init__(self):
     86        GeneratedClass.__init__(self)
     87        self.folderName = os.path.join(self.behaviorAbsolutePath(), "..")
     88        self.data = []
     89        with open(os.path.join(self.folderName, 'data.csv'), 'rb') as f:
     90            reader = csv.reader(f)
     91            for row in reader:
     92                self.data.append(row)
     93        self.logger.info("data: %d rows loaded" % len(self.data))
    7094}}}
    7195