Home » excel » How do I tell python the whole value is a string? since my excel formula has apostrophe in it

How do I tell python the whole value is a string? since my excel formula has apostrophe in it

Posted by: admin May 14, 2020 Leave a comment

Questions:

In python

formula = "=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1"

print (formula)

I GET!!

File "inventory change formula", line 52
    formula = "=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1""
                                                                                                                   ^
SyntaxError: invalid syntax

My question is how do i tell python the whole value is a string because my formula has apostrophes in it??

How to&Answers:

Use escape character

formula = "=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, \"=1\",'INVENTORY IN AND OUT'!D:D,\"=A\",'INVENTORY IN AND OUT'!C:C,\"=1\") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, \"=1\",'INVENTORY IN AND OUT'!D:D,\"=A\",'INVENTORY IN AND OUT'!C:C,\"=1"

print (formula)

Output

=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1

Answer:

Start and end your whole string with '''(triple quotes).

>>> formula = '''=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1'''

Output:

'=sumifs(\'INVENTORY IN AND OUT\'!F:F, \'INVENTORY IN AND OUT\'!E:E, "=1",\'INVENTORY IN AND OUT\'!D:D,"=A",\'INVENTORY IN AND OUT\'!C:C,"=1") - sumifs(\'INVENTORY IN AND OUT\'!G:G, \'INVENTORY IN AND OUT\'!E:E, "=1",\'INVENTORY IN AND OUT\'!D:D,"=A",\'INVENTORY IN AND OUT\'!C:C,"=1'

Answer:

You have to escape the inverted commas appropriately. Also, I did notice that you have forgotten the last parenthese in the string.

Try tris:

formula = "=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, \"=1\",'INVENTORY IN AND OUT'!D:D,\"=A\",'INVENTORY IN AND OUT'!C:C,\"=1\") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, \"=1\",'INVENTORY IN AND OUT'!D:D,\"=A\",'INVENTORY IN AND OUT'!C:C,\"=1\")"

print(formula)

Output:

=sumifs('INVENTORY IN AND OUT'!F:F, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1") - sumifs('INVENTORY IN AND OUT'!G:G, 'INVENTORY IN AND OUT'!E:E, "=1",'INVENTORY IN AND OUT'!D:D,"=A",'INVENTORY IN AND OUT'!C:C,"=1")