WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-19-2008, 09:58 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #2  
Old 04-19-2008, 10:48 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #3  
Old 04-19-2008, 01:54 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #4  
Old 04-21-2008, 07:03 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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.
Reply With Quote
  #5  
Old 07-07-2008, 01:38 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #6  
Old 07-07-2008, 02:49 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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.
Reply With Quote
  #7  
Old 07-07-2008, 03:14 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #8  
Old 07-08-2008, 11:27 AM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
  #9  
Old 07-08-2008, 12:02 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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.
Reply With Quote
  #10  
Old 07-08-2008, 02:16 PM
Uttama_vizard Uttama_vizard is offline
Member
 
Join Date: Sep 2007
Posts: 60
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
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not find plugin to load objects... halley Vizard 1 05-30-2006 11:01 AM
file input vsully Vizard 1 12-21-2004 10:51 PM
avatar head texture issues rconrey Vizard 3 11-17-2004 04:05 PM
Closing file handles from within function FlyingWren Vizard 1 10-01-2003 06:02 PM
I cannot show wrl file with text node sled Vizard 3 06-25-2003 07:52 AM


All times are GMT -7. The time now is 08:11 AM.


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