Python Forum
If statement containing apostrophe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If statement containing apostrophe (/thread-19637.html)

Pages: 1 2


If statement containing apostrophe - AshBax - Jul-08-2019

Hi,
I'm looking to compare strings (that have been pulled in from a csv) to a string created from an input.
All works fine, until the string contains an apostrophe.

so e.g if the csv contains "I don't know", and the user has typed "I don't know", Python is comparing:
csv:  'I don't know'
user input : 'I don\\'t know'

note - previously I wrote:
user input : 'I don\\'t know\\''
any thoughts on how to get past this?

many thanks in advance


RE: If statement containing apostrophe - ichabod801 - Jul-08-2019

The problem there isn't the apostrophe, it's that that there are two apostrophes in the user's input and only one in the csv data.


RE: If statement containing apostrophe - AshBax - Jul-08-2019

Thanks for the quick response..
The issue remains, but I have amended the original post to reflect this


RE: If statement containing apostrophe - ichabod801 - Jul-08-2019

That doesn't look like a problem, that just looks like how python represents quotes in strings. Show us the code that is loading the csv file, show us the code that is doing the comparison, and show us the relevant lines from the csv file.


RE: If statement containing apostrophe - buran - Jul-08-2019

Post your actual code where you compare strings. I guess the problem is (i)There is new line char at the end of the line from csv file and (ii) you don't need to escape the apostrophe in the user input


RE: If statement containing apostrophe - snippsat - Jul-08-2019

AshBax Wrote:note - previously I wrote:
user input : 'I don\\'t know\\'
Write it as you would do normally in user input.
>>> user_input = input('Type something: ')
Type something: i don't know

>>> user_input
"i don't know"

# To see all use repr
>>> repr(user_input)
'"i don\'t know"'
If you don't mess up the csv write stuff,it will compare to True.
>>> csv = "i don't know"
>>> repr(csv)
'"i don\'t know"'

>>> user_input == csv
True



RE: If statement containing apostrophe - AshBax - Jul-08-2019

Thanks.
the code is:
 answer = input(gameplay_A[i] + "\n")
         if answer.lower() == gameplay_B[i].lower():
                 print("\n\t ** Correct ** \n\n")



RE: If statement containing apostrophe - snippsat - Jul-08-2019

You don't show code that we cant test and what's gameplay_A[i], gameplay_B[i]?
You probably should not loop like this gameplay_A[i].

Just to make something similar that run.
>>> gameplay_A = ['Question:_1', 'Question:_2']
... lst = []
... for i,item in enumerate(gameplay_A):
...     answer = input(gameplay_A[i] + "\n")
...     lst.append(answer)
Question:_1
i don't know
Question:_2
hello world

>>> lst
["i don't know", "hello world"]
If gameplay_B[i] list contains the same it's True.
>>> gameplay_B = ["i don't know", "hello world"]

>>> lst == gameplay_B
True



RE: If statement containing apostrophe - AshBax - Jul-09-2019

firstly - thanks to everyone who has kindly looked at this.

in the code:
answer = input(gameplay_A[i] + "\n")
        if answer.lower() == gameplay_B[i].lower():
                print("\n\t ** Correct ** \n\n")
gameplay_A and gameplay_B refer to a list created from a csv file.
the exact line from the csv file is
Quote:I don’t know

I believe one issue maybe when input() a word(string) with an apostrophe this causes some confusion.

I've enclosed a version of the code with the same issue:
import sys
def open_file(file_name, state):
    try:
        the_file = open(file_name, state)
    except IOError as e:
        print("unable to open file")
        sys.exit()
    else:
        return the_file


def read_line(line, list_a, list_b):
    line = line.replace("\n", "")
    fields = line.split(",")
    list_a.append(fields[0])
    list_b.append(fields[1])
    return list_a, list_b


gameplay_A, gameplay_B = [],[]

this_file = open_file("know.csv", "r")

for i in this_file:
    gameplay_A, gameplay_B = read_line(i, gameplay_A, gameplay_B)

    print(gameplay_B)

for i in range(0,len(gameplay_A)):
    answer = input(gameplay_A[i] + "\n")
    if answer.lower() == gameplay_B[i].lower():
        print("\n\t ** Correct ** \n\n")
    else:
        print("WRONG!  i want to see : " + gameplay_B[i])
Output:
['I don’t know'] guess I don't know WRONG! i want to see : I don’t know Process finished with exit code 0
and the line from the csv file is:
guess,I don’t know


RE: If statement containing apostrophe - buran - Jul-09-2019

You removed (see a note below) the new-line ending from the line from csv file (line 13), and then on line 30 you add new-line '\n' to user input on line 30. So they cannot be equal.

Now for the line 13 - it's not a problem in this particular case, but you replace '\n' with '', but depending on OS and the way you open the file, the new line may differ, e.g. '\r\n' vs '\n' vs '\r'. better use line = line.strip() or line = line.rstrip()