-2

CREATE TABLE "USER "."ACT" ( "ACTNO" SMALLINT NOT NULL, "ACTKWD" CHAR(6 OCTETS) NOT NULL, "ACTDESC" VARCHAR(20 OCTETS) NOT NULL) IN "USERSPACE1" ORGANIZE BY ROW;

I need to replace or trim spaces between the double quotes "user ", how do I do it in python.

The expected output for the given input string is

CREATE TABLE "USER"."ACT" ( "ACTNO" SMALLINT NOT NULL, "ACTKWD" CHAR(6 OCTETS) NOT NULL, "ACTDESC" VARCHAR(20 OCTETS) NOT NULL) IN "USERSPACE1" ORGANIZE BY ROW;

If you see in the output after the string "user" space was removed.

MageshJ
  • 33
  • 6

3 Answers3

1
use strip()
var="user "
print(var.strip())
  • Thanks for the quick response, but 'user ' is not fixed, it may vary, only create or replace table is fixed. the goal is to remove string between double quotes – MageshJ Jun 01 '21 at 06:50
0

You can use regex to find double-quoted occurrences first and then remove the spaces accordingly

import re
words = re.findall(r'".*?"', text)
for word in words:
    text = text.replace(word, word.replace(" ", "")) 

OUTPUT:

'CREATE TABLE "USER"."ACT" ( "ACTNO" SMALLINT NOT NULL, "ACTKWD" CHAR(6 OCTETS) NOT NULL, "ACTDESC" VARCHAR(20 OCTETS) NOT NULL) IN "USERSPACE1" ORGANIZE BY ROW;'
ThePyGuy
  • 13,387
  • 4
  • 15
  • 42
-2

As I understand you want to get rid of the whitespace at the ends of a string. The function .strip() does this. Example:

var = "  user  "
new_var = var.strip()
print(new_var)

Output:

'user'
  • Thanks for quick response, but 'user ' is not fixed, it may vary, only create or replace table is fixed. the goal is to remove string between double quotes – MageshJ Jun 01 '21 at 06:49
  • @MageshJ Does that mean you want just the whitespace in `'user '` (so `' '`) or do you want to turn `'user '` into a completely empty string (`''`)? If you just want the whitespace, slicing the string would work. – Party-with-Programming Jun 01 '21 at 06:55
  • I need to remove or trim space between double quote "user "."tablename" should be "user"."tablename" – MageshJ Jun 01 '21 at 06:59
  • @MageshJ is the period between `user` and `tablename` supposed to be part of the string, so `'user .tablename'` (and that you want it to be `'user.tablename'`?) or is the period supposed to be between strings? This link might be helpful https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string – Party-with-Programming Jun 01 '21 at 07:10