0

So I'm using approach in this post to extract a double quoted string from a string. If the input string comes from terminal argument, it works fine. But if the input string comes from a txt file like the following, it gives nontype error. I tried to get the hash code for two strings(one from file and one from terminal) with identical txt content, and turns out they are different. I'm curious if anyone knows how to solve this?(in Python 3.x)

That said, I have set the default encoding to "utf-8" in my code.

python filename.py < input.txt
Taku
  • 31,927
  • 11
  • 74
  • 85
Grey258
  • 31
  • 3

3 Answers3

0

If you are using command python, the command recognize it to python 2.x.

If you want python 3.x, just change the command to python3 like this

python3 filename.py < input.txt
Voro
  • 235
  • 1
  • 12
0

Two things, if you want to ingest a txt file into a python script, you need to specify it. Add these two lines

import sys
text = str(sys.argv[1])

this mean text would be your 'input.txt'.

Second, if your script has only a function, it would not know what you want to do with the function, you have to either, tell the script explicity to execute the function through the entry main

import re
import sys
def doit(text):      
  matches=re.findall(r'\"(.+?)\"',text)
  # matches is now ['String 1', 'String 2', 'String3']
  return ",".join(matches)

if __name__ == '__main__':
   text_file = str(sys.argv[1])
   text = open(text_file).read()
   print(doit(text))

Alternately, you can just execute line by line without wrapping the re in a function, since it is only one line.

chrisckwong821
  • 1,133
  • 12
  • 24
0

I just figure it out, the bug doesn't come from my code. I had the "smart quotes" enabled on my Mac so whenever it reads a quote, it's identified as a special character. Disable this under keyboard setting would do the trick.

LOL what a "bug".

Grey258
  • 31
  • 3