-3

I am trying out text2bin.py file from the below link:

https://github.com/tensorflow/models/blob/master/swivel/text2bin.py

I had to modify some codes since it was written in python 2.7, but I need 3+. Anyway, I was trying out the code and realized that the sys.argv[1:] is currently empty.

import getopt
import os
import struct
import sys

try:
  opts, args = getopt.getopt(sys.argv[1:], 'o:v:', ['output=', 'vocab='])
except getopt.GetoptError as e:
    print (e, file = sys.stderr)
    sys.exit(2)

Below is my code for the whole model.

import getopt
import os
import struct
import sys

try:
  opts, args = getopt.getopt(sys.argv[1:], 'o:v:', ['output=', 'vocab='])

except getopt.GetoptError as e:
    print (e, file = sys.stderr)
    sys.exit(2)


opt_output = 'vecs.bin'
opt_vocab = 'vocab.txt'
for o, a in opts:
  if o in ('-o', '--output'):
    opt_output = a
  if o in ('-v', '--vocab'):
    opt_vocab = a
def go(fhs):
  fmt = None
  with open(opt_vocab, 'w') as vocab_out:
    with open(opt_output, 'wb') as vecs_out:
      for lines in list(zip(fhs)):
        parts = [str(line).split() for line in lines]
        token = parts[0][0]
        if any(part[0] != token for part in parts[1:]):
          raise IOError('vector files must be aligned')

        print(token, file = vocab_out)

        vec = [sum(float(x) for x in xs) for xs in list(zip((parts)[1:]))]


        if not fmt:
          fmt = struct.Struct('%df' % len(vec))


        vecs_out.write(fmt.pack())

if args:
  fhs = [open(filename) for filename in args]
  go(fhs)
  for fh in fhs:
    fh.close()
else:
  go([sys.stdin])

Why am I getting an empty sys.argv[1:]? Also, is my conversion from python 2.7 to 3.5 correct??

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Jason Han
  • 93
  • 1
  • 11

1 Answers1

0

I'm not sure if this was answered clearly but it sounds like you were not able to successfully pass in parameters. If you use sys.argv, you're taking in arguments from standard input and passing that to your program. I'm not familiar with the Windows command prompt but in Linux, you pass in parameters by making your Python file executable and then doing

./text2bin.py parameter1 parameter2.... (to execute your program with the parameters)

  • ./textbin.py <- runs the program and is considered to be sys.argv[0]
  • parameter1 <- considered to be sys.argv[1]
  • parameter2 <-considered to be sys.argv[2]

When you do sys.argv[1:] you are slicing off the first argument (sys.argv[0]) and grabbing the rest of the arguments you are passing to standard input along with executing the program.

Walton Lee
  • 50
  • 4
  • what would the parpameters be? sorry i'm very new to this whole coding world... would parameters be like ingesting files? – Jason Han May 01 '17 at 13:40