简体   繁体   中英

Unable to read image features - Python Path Error

I am trying to read image features from a publicly available dataset. The author has provided code for importing the image data (available in the .b format). However, when I try to specify path, I keep getting an "invalid syntax" error.

I have tried multiple ways of specifying path, including single quotes, double quotes, shifting the file to the working directory, but the error still persists.

Code provided by author:

import array

def readImageFeatures(path): 
  f = open(path, 'rb') 
   while True: 
    asin = f.read(10) 
    if asin == '': break 
    a = array.array('f') 
    a.fromfile(f, 4096) 
    yield asin, a.tolist() 

I replaced path with image_features_Baby.b after shifting the .b file to the working directory.

Error Message:

File "<ipython-input-11-64c836f0cfe3>", line 1 <br>
def readImageFeatures('image_features_Baby.b'): <br>
                                            ^
SyntaxError: invalid syntax <br>

I ran the code

import array

def readImageFeatures(path): 
    f = open(path, 'rb') 
    while True: 
      asin = f.read(10) 
      if asin == '': 
          break 
      a = array.array('f') 
      a.fromfile(f, 4096) 
      yield asin, a.tolist() 

readImageFeatures('image_features_Baby.b')

and it worked fine. The only thing i did was clean up the tabs/spaces. That may be your issue. I also put your break on a new line but that shouldn't be your error.

I did have to fix these lines though:

f = open(path, 'rb') 
 while True:

to

f = open(path, 'rb') 
while True:

for an indent issue. Also if you are trying to do:

import array

def readImageFeatures('some_file_here.txt'): 
   f = open(path, 'rb') 
   while True: 
    asin = f.read(10) 
    if asin == '': break 
    a = array.array('f') 
    a.fromfile(f, 4096) 
    yield asin, a.tolist() 

than read the comment pinned to the question about the difference between a call and defining the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM