convert a greek file name into latin one in Python

Multi tool use
I would be gratefull if someone could help me with this problem!
I have an empty file (in real program there is some data in it) and its name contains greek letters e.g. "Σ2262.txt". My goal is to convert all greek letters in latin, for example the greek letter "Σ" to latin "S". My code is as follows:
for dirpath, dirnames, filenames in os.walk(FOLDER_PATH, topdown=True):
for name in filenames:
if name.endswith('2262.txt'):
old_name = name
#new = re.sub('Σ', 'S', name)
for char in name:
print char
#print grToLat.get(char)
name = re.sub(char, grToLat.get(char), name)
print "GrToLat: "+ name
os.rename(os.path.join(dirpath, old_name),os.path.join(dirpath, name))
I have also constructed a dictionary that maps each greek letter to the corresponding latin one as below:
grToLat = {
'Α':'A', 'α':'a', 'ά':'a', 'Β':'B', 'β':'b',
'Γ':'G', 'γ':'g', 'Δ':'D', 'δ':'d', 'Ε':'E',
'έ':'e', 'ε':'e', 'Ζ':'Z', 'ζ':'z', 'Η':'H',
'η':'h', 'ή':'h', 'Θ':'U', 'θ':'u', 'Ι':'I',
'ι':'i', 'ί':'i', 'Κ':'K', 'κ':'k', 'Λ':'L',
'λ':'l', 'Μ':'M', 'μ':'m', 'Ν':'N', 'ν':'n',
'Ξ':'J', 'ξ':'j', 'Ο':'O', 'ο':'o', 'ό':'o',
'Π':'P', 'π':'p', 'Ρ':'R', 'ρ':'r', 'Σ':'S',
'ς':'s', 'σ':'s', 'Τ':'T', 'τ':'t', 'Υ':'Y',
'ύ':'y', 'υ':'y', 'Φ':'F', 'φ':'f', 'Χ':'X',
'χ':'x', 'Ψ':'C', 'ψ':'c', 'ω':'v', 'ώ':'v',
'Ω':'V'
}
where FOLDER_PATH contains my file.
With the above code It seems that the greek letter "Σ" does not map to any latin letter. What is strange about this is that even if we uncomment the line and try it hardcoded nothing happens again!
new = re.sub('Σ', 'S', name)
What am I doing wrong?
I am using python2 my operating system is windows 10.
regex python-2.7
add a comment |
I would be gratefull if someone could help me with this problem!
I have an empty file (in real program there is some data in it) and its name contains greek letters e.g. "Σ2262.txt". My goal is to convert all greek letters in latin, for example the greek letter "Σ" to latin "S". My code is as follows:
for dirpath, dirnames, filenames in os.walk(FOLDER_PATH, topdown=True):
for name in filenames:
if name.endswith('2262.txt'):
old_name = name
#new = re.sub('Σ', 'S', name)
for char in name:
print char
#print grToLat.get(char)
name = re.sub(char, grToLat.get(char), name)
print "GrToLat: "+ name
os.rename(os.path.join(dirpath, old_name),os.path.join(dirpath, name))
I have also constructed a dictionary that maps each greek letter to the corresponding latin one as below:
grToLat = {
'Α':'A', 'α':'a', 'ά':'a', 'Β':'B', 'β':'b',
'Γ':'G', 'γ':'g', 'Δ':'D', 'δ':'d', 'Ε':'E',
'έ':'e', 'ε':'e', 'Ζ':'Z', 'ζ':'z', 'Η':'H',
'η':'h', 'ή':'h', 'Θ':'U', 'θ':'u', 'Ι':'I',
'ι':'i', 'ί':'i', 'Κ':'K', 'κ':'k', 'Λ':'L',
'λ':'l', 'Μ':'M', 'μ':'m', 'Ν':'N', 'ν':'n',
'Ξ':'J', 'ξ':'j', 'Ο':'O', 'ο':'o', 'ό':'o',
'Π':'P', 'π':'p', 'Ρ':'R', 'ρ':'r', 'Σ':'S',
'ς':'s', 'σ':'s', 'Τ':'T', 'τ':'t', 'Υ':'Y',
'ύ':'y', 'υ':'y', 'Φ':'F', 'φ':'f', 'Χ':'X',
'χ':'x', 'Ψ':'C', 'ψ':'c', 'ω':'v', 'ώ':'v',
'Ω':'V'
}
where FOLDER_PATH contains my file.
With the above code It seems that the greek letter "Σ" does not map to any latin letter. What is strange about this is that even if we uncomment the line and try it hardcoded nothing happens again!
new = re.sub('Σ', 'S', name)
What am I doing wrong?
I am using python2 my operating system is windows 10.
regex python-2.7
2
You certainly don't need a regex for that job..replace()
would suffice.
– Austin
Dec 27 '18 at 15:50
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
1
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
You need to prefix all strings withu
prefix.
– Wiktor Stribiżew
Dec 27 '18 at 19:04
add a comment |
I would be gratefull if someone could help me with this problem!
I have an empty file (in real program there is some data in it) and its name contains greek letters e.g. "Σ2262.txt". My goal is to convert all greek letters in latin, for example the greek letter "Σ" to latin "S". My code is as follows:
for dirpath, dirnames, filenames in os.walk(FOLDER_PATH, topdown=True):
for name in filenames:
if name.endswith('2262.txt'):
old_name = name
#new = re.sub('Σ', 'S', name)
for char in name:
print char
#print grToLat.get(char)
name = re.sub(char, grToLat.get(char), name)
print "GrToLat: "+ name
os.rename(os.path.join(dirpath, old_name),os.path.join(dirpath, name))
I have also constructed a dictionary that maps each greek letter to the corresponding latin one as below:
grToLat = {
'Α':'A', 'α':'a', 'ά':'a', 'Β':'B', 'β':'b',
'Γ':'G', 'γ':'g', 'Δ':'D', 'δ':'d', 'Ε':'E',
'έ':'e', 'ε':'e', 'Ζ':'Z', 'ζ':'z', 'Η':'H',
'η':'h', 'ή':'h', 'Θ':'U', 'θ':'u', 'Ι':'I',
'ι':'i', 'ί':'i', 'Κ':'K', 'κ':'k', 'Λ':'L',
'λ':'l', 'Μ':'M', 'μ':'m', 'Ν':'N', 'ν':'n',
'Ξ':'J', 'ξ':'j', 'Ο':'O', 'ο':'o', 'ό':'o',
'Π':'P', 'π':'p', 'Ρ':'R', 'ρ':'r', 'Σ':'S',
'ς':'s', 'σ':'s', 'Τ':'T', 'τ':'t', 'Υ':'Y',
'ύ':'y', 'υ':'y', 'Φ':'F', 'φ':'f', 'Χ':'X',
'χ':'x', 'Ψ':'C', 'ψ':'c', 'ω':'v', 'ώ':'v',
'Ω':'V'
}
where FOLDER_PATH contains my file.
With the above code It seems that the greek letter "Σ" does not map to any latin letter. What is strange about this is that even if we uncomment the line and try it hardcoded nothing happens again!
new = re.sub('Σ', 'S', name)
What am I doing wrong?
I am using python2 my operating system is windows 10.
regex python-2.7
I would be gratefull if someone could help me with this problem!
I have an empty file (in real program there is some data in it) and its name contains greek letters e.g. "Σ2262.txt". My goal is to convert all greek letters in latin, for example the greek letter "Σ" to latin "S". My code is as follows:
for dirpath, dirnames, filenames in os.walk(FOLDER_PATH, topdown=True):
for name in filenames:
if name.endswith('2262.txt'):
old_name = name
#new = re.sub('Σ', 'S', name)
for char in name:
print char
#print grToLat.get(char)
name = re.sub(char, grToLat.get(char), name)
print "GrToLat: "+ name
os.rename(os.path.join(dirpath, old_name),os.path.join(dirpath, name))
I have also constructed a dictionary that maps each greek letter to the corresponding latin one as below:
grToLat = {
'Α':'A', 'α':'a', 'ά':'a', 'Β':'B', 'β':'b',
'Γ':'G', 'γ':'g', 'Δ':'D', 'δ':'d', 'Ε':'E',
'έ':'e', 'ε':'e', 'Ζ':'Z', 'ζ':'z', 'Η':'H',
'η':'h', 'ή':'h', 'Θ':'U', 'θ':'u', 'Ι':'I',
'ι':'i', 'ί':'i', 'Κ':'K', 'κ':'k', 'Λ':'L',
'λ':'l', 'Μ':'M', 'μ':'m', 'Ν':'N', 'ν':'n',
'Ξ':'J', 'ξ':'j', 'Ο':'O', 'ο':'o', 'ό':'o',
'Π':'P', 'π':'p', 'Ρ':'R', 'ρ':'r', 'Σ':'S',
'ς':'s', 'σ':'s', 'Τ':'T', 'τ':'t', 'Υ':'Y',
'ύ':'y', 'υ':'y', 'Φ':'F', 'φ':'f', 'Χ':'X',
'χ':'x', 'Ψ':'C', 'ψ':'c', 'ω':'v', 'ώ':'v',
'Ω':'V'
}
where FOLDER_PATH contains my file.
With the above code It seems that the greek letter "Σ" does not map to any latin letter. What is strange about this is that even if we uncomment the line and try it hardcoded nothing happens again!
new = re.sub('Σ', 'S', name)
What am I doing wrong?
I am using python2 my operating system is windows 10.
regex python-2.7
regex python-2.7
asked Dec 27 '18 at 15:47
bettas
11029
11029
2
You certainly don't need a regex for that job..replace()
would suffice.
– Austin
Dec 27 '18 at 15:50
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
1
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
You need to prefix all strings withu
prefix.
– Wiktor Stribiżew
Dec 27 '18 at 19:04
add a comment |
2
You certainly don't need a regex for that job..replace()
would suffice.
– Austin
Dec 27 '18 at 15:50
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
1
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
You need to prefix all strings withu
prefix.
– Wiktor Stribiżew
Dec 27 '18 at 19:04
2
2
You certainly don't need a regex for that job.
.replace()
would suffice.– Austin
Dec 27 '18 at 15:50
You certainly don't need a regex for that job.
.replace()
would suffice.– Austin
Dec 27 '18 at 15:50
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
1
1
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
You need to prefix all strings with
u
prefix.– Wiktor Stribiżew
Dec 27 '18 at 19:04
You need to prefix all strings with
u
prefix.– Wiktor Stribiżew
Dec 27 '18 at 19:04
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947549%2fconvert-a-greek-file-name-into-latin-one-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947549%2fconvert-a-greek-file-name-into-latin-one-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
iTtwl2Q0Edqr,OW9pytcGGenod
2
You certainly don't need a regex for that job.
.replace()
would suffice.– Austin
Dec 27 '18 at 15:50
What is your OS?
– usr2564301
Dec 27 '18 at 16:01
1
Have you tried Python 3? In Python 2 you're probably getting the file names in an incompatible encoding.
– user4815162342
Dec 27 '18 at 16:02
windows 10 @usr2564301
– bettas
Dec 27 '18 at 16:02
You need to prefix all strings with
u
prefix.– Wiktor Stribiżew
Dec 27 '18 at 19:04