WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Read file....many issues (https://forum.worldviz.com/showthread.php?t=1454)

Uttama_vizard 04-19-2008 09:58 AM

Read file....many issues
 
I am working with a text file and trying to read it.

The text file is as follows:

Data properties:
.
.few lines
.

5 1 -1280 -1024 -1.000 -1.000 -1.000 -1.000 4 -1280 -1024 -1.000 -1.000 -1.000 -1.000 4
.
.
.
My 21st line starts with '5' as shown. From this line onwards, the data is important to me and which I want to read.

1st question:

file = open('Test.txt', 'r')
f = file.read()
print f

These lines of code prints whatever is in the text file. But, I want to selectively pick up the lines important to me (say from 21st line onwards).

2nd question:

string1 = []
string2 = []
for line in file: s = line.split() string1.append(s[0]) print string1 string2.append(s[1]) print 'abcd'
file.close()

When I am using this code, I am getting the following error message:

string1.append(s[0])
IndexError: list index out of range

But, I want to gain access to all (say about 16 strings).

Question 3:

I want to use these string values, to initiate some other actions. How do I do that.

Question 4:

I want to access this text file from another computer which is generating this data from some other application. Is there any easy way, by which I can get access to this text file on a real time basis while working in the vizard environment.

Sorry to ask so many questions...what to do...simply got stuck up in solving these issues.

If somebody can give me some valuable suggestions...that would be wonderful.

Thanks

Uttama

Uttama_vizard 04-19-2008 10:48 AM

Read File....some issues
 
If we use

import linecache

then, we use

q =linecache.getline('Test.txt', 22)
print q

This prints the line 22 selectively from the bulk of data.

This answers the first question.

If somebody can help in solving the other 3, that would be wonderful.

Thanks

Uttama

Uttama_vizard 04-19-2008 01:54 PM

Read File....many issues
 
The text file missed some of the lines....so there was an error. The problem is solved.

But, the problem that still remains is the fourth question. Accessing a text file from a remote computer which is generating the text files. I want to access it while I am working in the vizard environment (in a different computer) on a real time basis.

Thanks

Uttama

farshizzo 04-21-2008 07:03 PM

You can open a file any time while your script is running. If the file is being updated at a certain rate, then you can setup a timer in Vizard that will open the file and read the contents.

Uttama_vizard 07-07-2008 01:38 PM

Hello,

Yes, I am trying to open a txt file many times and read its contents. But, I am facing a typical problem.

My application is to sense the repeated pressing of a particular key of the keyboard. What I am doing is, each time I am sending a letter and writing to the txt file (response1.txt) and then trying to open the file in the read mode to read its contents.

When I am using these lines at the beginning of my program (outside all loops), I am being able to read the .txt file but, not its updated contents.

file5 = open('response1.txt', 'w')
file6 = open('response1.txt', 'r+')

When I am trying to close the file and open it within a loop such as :

while count<1:
vizact.onkeydown('e',InstructionScreenOFF) #Do an action on 'e' key press
yield viztask.waitTime(10)
file6 = open('response1.txt', 'r+')


I am getting the error message..................

TypeError: 'ActionData' object is not callable

Without opening the file .txt multiple times within the program, I cannot access its updated contents....and there I am getting into trouble.

If somebody can suggest me any solution for this.....it would be really helpful to me.

Thanks

Uttama

farshizzo 07-07-2008 02:49 PM

Can you post the full traceback of your error? Python errors are usually very descriptive and will tell you exactly which line of code caused the error.

Uttama_vizard 07-07-2008 03:14 PM

Hello,

This is a portion of my code....

import viz
import vizinfo
import viztask
import vizmat
import vizact

viz.go()

#These are for initializing the file
file5 = open('response_demo1_repeated_d_button.txt', 'w')
out = ''
file5.write(out)
file5.flush()

key = None
lastKey = None
lastKeyTime = 0.0
timer1 = 1
timer2 = 2

def StartStory():

#Set variables
count = 0
pool_number = 3
x_count = 0
y_count = 0

yield viztask.waitTime(2)
sound_Instruction1.play()

while count<1:

....some actions the avatar is performing...

yield onKeyDown(key)
.
.
.


def onKeyDown(key):
global lastKey,lastKeyTime

elapsed = viz.tick() - lastKeyTime

file6 = open('response_demo1_repeated_d_button.txt', 'r+')

if key == 'd' and lastKey != 'a' and lastKey != 'b' and lastKey != 'c' and elapsed < 100:
out = 'n'
file5.write(out)
file5.flush()

lastKey = key
lastKeyTime = viz.tick()

viz.callback(viz.KEYDOWN_EVENT,onKeyDown)

The error which I am getting....

Traceback (most recent call last):
File "Sum08_Demo1_NEC_1A_4p5F.py", line 1880, in onKeyDown
file6 = open('response_demo1_repeated_d_button.txt', 'r+') # opens file 'response_demo1_instructionscreen.txt' for reading
TypeError: 'ActionData' object is not callable


The code in blue is at line 1880.
In fact, when on multiple press of my 'd' key, I am seeing that the .txt file has a number of 'n'. I am trying to read these muliple 'n' (representing 'd' key press).

When I am placing the blue line on top, there is no error, but, in that case I cannot open my .txt file multiple times to get its updated contents. Actually, on every press of 'd' key, I will get an additional 'n' which I want to read.

Actually I am confused with the ActionData error when simply I am trying to open a .txt file.

Thanks in advance for the suggestion.

Uttama

Uttama_vizard 07-08-2008 11:27 AM

Hello,

I am facing the problem of multiple access (precisely...in opening a .txt file in read mode multiple times) so that I can get the updated contents of the .txt file each time.

The error message which I am getting is due to my trying to open the .txt file in read mode multiple times within a loop. The error message which I am getting is in my last posting. I don't understand, how it is being deciphered by the program as ActioData.

Hope I made my question clear.........

If anyone has any suggestion....it would be really helpful to me.

Thanks

Uttama

farshizzo 07-08-2008 12:02 PM

That error is telling you that you have an ActionData object and you are trying to call it like a function, which it does not support. Since the only function call happening on that line of code is the call to open, that means somewhere above that line you have assigned an ActionData object to a variable called open. I cannot tell you exactly where this is happening because you didn't post all the code, but I'm pretty sure this is what is causing the error.

Uttama_vizard 07-08-2008 02:16 PM

Hello Farshizzo,

Really there was a open variable (for mouth open) several lines ahead in the program (somehow I missed it.....)

Now I can open the file in write mode many times. Thanks for your brilliant suggestion.

Uttama


All times are GMT -7. The time now is 02:46 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC