-1

I have this helper function that gets rid of control characters in XML text:

def remove_control_characters(s): #Remove control characters in XML text
    t = ""
    for ch in s:
        if unicodedata.category(ch)[0] == "C":
            t += " "
        if ch == "," or ch == "\"":
            t += ""
        else:
            t += ch
    return "".join(ch for ch in t if unicodedata.category(ch)[0]!="C")

I would like to know whether there is a unicode category for excluding quotation marks and commas.

SANBI samples
  • 2,058
  • 2
  • 14
  • 20
  • 2
    This question is incomplete. Generally speaking, you can have question marks and commas in JSON data. I regularly pass XML documents as part of JSON data structures. So here you should show the input to your function, and show how you use the output in such a way that you get invalid JSON. – Louis Jun 29 '16 at 14:12
  • 3
    Add a sample of a problem piece of xml and expected output – Padraic Cunningham Jun 29 '16 at 20:07
  • The function takes a string, and the output is what I expect it to be. But what I want to know is whether there is a unicode category for commas and quotation marks. – SANBI samples Jul 04 '16 at 07:14

2 Answers2

1

In Unicode, control characters general category is 'Cc', even if they have no name.unicodedata.category() returns the general category, as you can test for yourself in the python console :

>>>unicodedata.category(unicode('\00')) 'Cc'

For commas and quotation marks, the categories are Pi and Pf. You only test the first character of the returned code in your example, so try instead :

 cat = unicodedata.category(ch)
 if cat == "Cc" or cat == "Pi" or cat == "Pf":
Xander
  • 96
  • 1
  • 9
1

Based on a last Unicode data file here UnicodeData.txt

Comma and Quotation mark are in Punctuation Other category Po:

002C;COMMA;Po;0;CS;;;;;N;;;;;
0022;QUOTATION MARK;Po;0;ON;;;;;N;;;;;

So, based on your question, your code should be something like this:

o = [c if unicodedata.category(c) != 'Cc' else ' '\
    for c in xml if unicodedata.category(c) != 'Po']

return("".join(o))

If you want to find out a category for any other unicode symbol and do not want to deal with the UnicodeData.txt file, you can just print it out with a print(c, unicodedata.category(c))

Dmitry
  • 2,626
  • 2
  • 10
  • 15