-2

I am a student who is new to python and need to finish a python program for assignment. The goal of the program is to find some target strings in a list of news titles.

For example, there is a file written with a list of news title. When I type this in command line:

"python hw3.py Obama#Trump,Japan#anime,Hearthstone#Overwatch new_list.txt" ,

the program will return the numbers of titles which have both "Obama" and "Trump" in them, and also which have both "Japan" and "anime" in them, and so on.

However, when I was debugging my program, it seems that the command line can't read the symbol "#" because the words after # are all red, and the result when entering the line went "Error:syntax invalid".

Was the problem caused by the "#" symbol? Because typing "#" is the requirement of the assignment, so I must find a way to make it right. Or the problem was caused by other reason?

The code is here:

import sys

if len(sys.argv) < 3: 
    print 'no argument'
    sys.exit()

f = open(sys.argv[2], 'r', encoding = 'UTF-8')

words = sys.argv[1].split(',')
count = len(words)
for i in range(0, count, 1):
    yes[count] = 0
    word = words[count].decode('utf-8').split('#')
    while True :
         x = f.readline()
         if x == '':
             break
         #print(x,end='')

         else if (word[0] in x) and (word[1] in x):
             yes[count] += 1


for j in range (0, count, 1):
    t = max(yes)
    for jk in range (0, count, 1):
        if yes[jk] == t:
            print words[jk] + ',' + yes[jk]
            yes[jk] = -1
            break


f.close()

And the message is here:

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> python TocHW3.py 馬英九#蔡英文 new_sample.txt
SyntaxError: invalid syntax
Limoncool
  • 5
  • 4
  • 2
    What operating system do you use and which terminal? – DomTomCat Jun 14 '16 at 12:06
  • Windows 10, python 2 – Limoncool Jun 14 '16 at 12:07
  • You could just wrap your args with quotes, so the shell won't try to interprete them `python hw3.py "Obama#Trump,Japan#anime,Hearthstone#Overwatch" new_list.txt` – Cal Eliacheff Jun 14 '16 at 12:09
  • 1
    also you should update your question with a [minimal code example](http://stackoverflow.com/help/mcve), since I don't have this problem in windows – DomTomCat Jun 14 '16 at 12:10
  • 1
    What command line are you using? The windows cmd? Powershell? IDLE? – syntonym Jun 14 '16 at 12:10
  • @CalEliacheff Thx but sorry I can't change to that because the format is the requirement,too. – Limoncool Jun 14 '16 at 12:11
  • @DomTomCat Thx, I will post it with my code later, I am new here, so I am still learning how to use the site. – Limoncool Jun 14 '16 at 12:13
  • @syntonym IDLE python shell – Limoncool Jun 14 '16 at 12:15
  • 1
    `print sys.argv` to check if arguments are passed from shell to Python correctly. I bet they are not. – Łukasz Rogalski Jun 14 '16 at 12:15
  • 3
    The error you're getting seems like one the python interpreter gives, so are you starting an interactive python session and then at the `>>>` prompt typing all the `python hw.py ...` ? Try in a windows cmd prompt or powershell. The person who set your requirement needs educating that `#` is a comment symbol that in many shells will mean everything after it is ignored, their ignorance is showing in their requirements. – Simon Fraser Jun 14 '16 at 12:16
  • @SimonFraser I know that # is the comment symbol, so I am confused, too. However, this is the requirement. – Limoncool Jun 14 '16 at 12:25
  • @Limoncool the problem appears to be that you're typing that into the Python interpreter, **not** your terminal/command line. – jonrsharpe Jun 14 '16 at 12:32
  • 1
    @Limoncool Don't use IDLE to execute your script. Start a windows command prompt (cmd), go to the directory where you stored your python script (cd) and then type in `python myscript.py arg1 arg2 arg3 ...` (with the right name and arguments). In the windows cmd shell the `#` will *not* be recognized as a comment symbol. You are getting the error because you are trying to start python from python itself. – syntonym Jun 14 '16 at 12:32
  • @jonrsharpe thank u two, I am a newbie of python, so I thought that the shell is the terminal.....I am a fool. Thx for all answers, I will try in windows cmd now. – Limoncool Jun 14 '16 at 12:39
  • @syntonym thank u two, I am a newbie of python, so I thought that the shell is the terminal.....I am a fool. Thx for all answers, I will try in windows cmd now. – Limoncool Jun 14 '16 at 12:39

1 Answers1

0

If you'd like to execute a file in the python shell with parameters: you can simply use: subprocess.call(['./abc.py', arg1, arg2]) for reference: Execute a file with arguments in Python shell

Community
  • 1
  • 1
4Gred__
  • 96
  • 1
  • 3
  • 11