It is possible to use hex values in "sed".
echo "Ã" | hexdump -C
00000000 c3 83 0a |...|
00000003
Ok, that character is two byte combination "c3 83". Let's replace it with single byte "A":
echo "Ã" |sed 's/\xc3\x83/A/g'
A
Explanation: \x indicates for "sed" that a hex code follows.
Answer from ajaaskel on Stack OverflowIt is possible to use hex values in "sed".
echo "Ã" | hexdump -C
00000000 c3 83 0a |...|
00000003
Ok, that character is two byte combination "c3 83". Let's replace it with single byte "A":
echo "Ã" |sed 's/\xc3\x83/A/g'
A
Explanation: \x indicates for "sed" that a hex code follows.
Try setting LANG=C and then run it over the Unicode range:
echo "hi ☠ there ☠" | LANG=C sed "s/[\x80-\xFF]//g"
For converting to ASCII you might want to try ASCII, Dammit or this recipe, which boils down to:
>>> title = u"Klüft skräms inför på fédéral électoral große"
>>> import unicodedata
>>> unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'
- Use the
fileinputmodule to loop over standard input or a list of files, - decode the lines you read from UTF-8 to unicode objects
- then map any unicode characters you desire with the
translatemethod
translit.py would look like this:
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
import fileinput
table = {
0xe4: u'ae',
ord(u'ö'): u'oe',
ord(u'ü'): u'ue',
ord(u'ß'): None,
}
for line in fileinput.input():
s = line.decode('utf8')
print s.translate(table),
And you could use it like this:
$ cat utf8.txt
sömé täßt
sömé täßt
sömé täßt
$ ./translit.py utf8.txt
soemé taet
soemé taet
soemé taet
- Update:
In case you are using python 3 strings are by default unicode and you dont' need to encode it if it contains non-ASCII characters or even a non-Latin characters. So the solution will look as follow:
line = 'Verhältnismäßigkeit, Möglichkeit'
table = {
ord('ä'): 'ae',
ord('ö'): 'oe',
ord('ü'): 'ue',
ord('ß'): 'ss',
}
line.translate(table)
>>> 'Verhaeltnismaessigkeit, Moeglichkeit'
Replace unicode characters with useful ascii characters
ascii - Search and replace unicode character codes with actual characters - Vi and Vim Stack Exchange
How to REPLACE a known Unicode character by another (ASCII) character in Word 2016
java - Unicode Replacement with ASCII - Stack Overflow
Decode the string to Unicode. Assuming it's UTF-8-encoded:
str.decode("utf-8")Call the
replacemethod and be sure to pass it a Unicode string as its first argument:str.decode("utf-8").replace(u"\u2022", "*")Encode back to UTF-8, if needed:
str.decode("utf-8").replace(u"\u2022", "*").encode("utf-8")
(Fortunately, Python 3 puts a stop to this mess. Step 3 should really only be performed just prior to I/O. Also, mind you that calling a string str shadows the built-in type str.)
Encode string as unicode.
>>> special = u"\u2022"
>>> abc = u'ABC•def'
>>> abc.replace(special,'X')
u'ABCXdef'
I believe the problem is that Vim not opening the file using the right encoding. Your file is in encoding cp1252 but Vim doesn't detect it and guess utf-8.
I would propose to open the file with the ++enc=cp1252 flag.
:e ++enc=cp1252 filepath
To improve the way Vim guess the encoding you can add the following lines (Vim would first try to open as cp1252 and then as utf-8):
:set fileencodings=cp1252,utf-8
I would propose you:
:s/\([\x80-\xFF]\)/\='0x'.printf('%02x', char2nr(submatch(1)))/g
Word plays internal tricks with Unicode symbols, and doesn't report the true character values. To make successful replacements, use the macros in the article https://wordmvp.com/FAQs/MacrosVBA/FindReplaceSymbols.htm.
Hi Jay,
Thank you very much - the macros in the article worked just great!
This was the first time I ever dabbled with VBA in Word, and this worked almost first time. (I do have quite a lot of experience writing VBA macros in Excel, but never in Word.)
Something I learned from using these macros for my specific case - to find and replace all the ?-in-a-rectangles with a regular double quote - is that, in Word t,here is a difference between an opening double quote mark and a closing double quote mark.
At first I highlighted a double quote mark in my document (by chance, a closing one), and used the code that the first macro returned to replace ALL of the ?-in-a-rectangles. This caused the Word spelling & grammar checker to complain that, for all the replaced opening quotes, the adjacent space should go after the quote character. This made me realize that the opening & closing quotes had different codes, though in my document they looked the same. Only when I enlarged the font, was a difference between them discernable.
Also, only the opening ?-in-a-rectangle was replaced by the closing ". This made me very suspicious, so I used the first macro to display the code of the closing ?-in-a-rectangle - it also was different from the opening character code, which is what I'd used in the second macro.
So I undid all the replacements, and ran the second macro again, twice, each time with the corresponding codes for the opening & closing characters. (8220 --> 147, 8221 --> 148, all (normal text) font)
This time the result was perfect.
many thanks!.
Match the escape sequence \uXXXX with a regular expression. Then use a replacement loop to replace each occurrence of that escape sequence with the decoded value of the character.
Because Java string literals use \ to introduce escapes, the sequence \\ is used to represent \. Also, the Java regex syntax treats the sequence \u specially (to represent a Unicode escape). So the \ has to be escaped again, with an additonal \\. So, in the pattern, "\\\\u" really means, "match \u in the input."
To match the numeric portion, four hexadecimal characters, use the pattern \p{XDigit}, escaping the \ with an extra \. We want to easily extract the hex number as a group, so it is enclosed in parentheses to create a capturing group. Thus, "(\\p{XDigit}{4})" in the pattern means, "match 4 hexadecimal characters in the input, and capture them."
In a loop, we search for occurrences of the pattern, replacing each occurrence with the decoded character value. The character value is decoded by parsing the hexadecimal number. Integer.parseInt(m.group(1), 16) means, "parse the group captured in the previous match as a base-16 number." Then a replacement string is created with that character. The replacement string must be escaped, or quoted, in case it is $, which has special meaning in replacement text.
String data = "This is\\u2019 a sample text file \\u2014and it can ...";
Pattern p = Pattern.compile("\\\\u(\\p{XDigit}{4})");
Matcher m = p.matcher(data);
StringBuffer buf = new StringBuffer(data.length());
while (m.find()) {
String ch = String.valueOf((char) Integer.parseInt(m.group(1), 16));
m.appendReplacement(buf, Matcher.quoteReplacement(ch));
}
m.appendTail(buf);
System.out.println(buf);
If you can use another library, you can use apache commons https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringEscapeUtils.html
String dirtyString = "Colocaci\u00F3n";
String cleanString = StringEscapeUtils.unescapeJava(dirtyString);
//cleanString = "Colocación"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unicodedata
text = u'Cześć'
print unicodedata.normalize('NFD', text).encode('ascii', 'ignore')
The package unidecode worked best for me:
from unidecode import unidecode
text = "Björn, Łukasz and Σωκράτης."
print(unidecode(text))
# ==> Bjorn, Lukasz and Sokrates.
You might need to install the package:
pip install unidecode
The above solution is easier and more robust than encoding (and decoding) the output of unicodedata.normalize(), as suggested by other answers.
# This doesn't work as expected:
ret = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
print(ret)
# ==> b'Bjorn, ukasz and .'
# Besides not supporting all characters, the returned value is a
# bytes object in python3. To yield a str type:
ret = ret.decode("utf8") # (not required in python2)