2

edit below

First of all, I am quite sure this is a duplicated question. But I have no idea how to search for it. I spent quite a long time already.

I am reading with python from a .txt file the path to a specific file. i.e.

/home/username/Documents/K\"{u}hnelt.pdf

Now, if I try to copy it into another directory using shutil.copyfile(), it says it cannot find it. However, if I write manually

/home/username/Documents/Kühnelt.pdf

it works just fine. I assume it is an encoding issue, but I am not fluent with these things, and I cannot figure it out.

I am sorry if there are technical mistakes in the following question, but I don't know how to make it properly. I hope you can understand it correctly. The question is, is there any command in python that "interprets" the string as it is written in the command line, and converts it into a "fully composed" type string?

I am doing a script in python to copy several .pdf I have in my computer into the current folder. I actually read the pdf location from a .bib file. It is this .bib file which has the location writen in a TeX format.

Now, my python script reads the .bib file line by line, and when it finds

file = {:home/user/Documents/K\"{u}hnelt\_2003.pdf:pdf},

it parses it to keep MyString = "home/user/Documents/K\"{u}hnelt\_2003.pdf"

then I do shutil.copyfile(MyString, "NewName.pdf" ) which copies the file in the current directory. It works fine, except for files that contain this TeX style.

  • What does the .txt file contain exactly? K\"{u}hnelt.pdf and Kühnelt.pdf are not the same thing, except (to some extent) in TeX. What is the output of ls /home/username/Documents/Kühnelt.pdf | od -t x1 (with the working path) and of sed -n 42p somefile.txt | od -t x1 with somefile.txt replaced by the name of the .txt file and 42 replaced by the line number containing the file name? – Gilles Apr 26 '15 at 18:15
  • Thank you for the answer @Gilles. The .txt file contains "K\"{u}hnelt.pdf". Output of ./Kühnelt.pdf | od -t x1 is: 0000000 2e 2f 4b c3 bc 68 6e 65 6c 74 2e 70 64 66 0a 0000017 And sed -n 42p somefile.txt | od -t x1 with somefile.txt: 0000000 2f 68 6f 6d 65 2f 75 73 65 72 2f 74 65 6d 70 2f 0000020 4b 5c 22 7b 75 7d 68 6e 65 6c 74 2e 70 64 66 0a 0000040 where the corresponding line in the .txt is /home/user/temp/K\"{u}hnelt.pdf – manolius Apr 26 '15 at 18:29
  • @manolius please edit your question to add extra information, it is hard to read and easy to miss int he comments. You need to show us the script you're using (or at least, the part of it necessary to understand what you're doing). If I understand correctly, you seem to think that the TeX \"{u} == ü which is not the case. Why are you using TeX-style? Why doesn't your text file contain /home/username/Documents/Kühnelt.pdf? Anyway, please edit and clarify. – terdon Apr 26 '15 at 18:37
  • Ok, so you have a file name in UTF-8 (fine), but a text file containing a file name with TeX encoding (strange). What produces this text file? It's likely to be easier and more reliable to produce a UTF-8 file name in the first place than to try to guess exactly how the text file is encoded. – Gilles Apr 26 '15 at 18:44
  • @Gilles Thank you, I added some extension in the question. I hope it helps – manolius Apr 26 '15 at 18:57
1

Consider moving away from the antiquitated, non-Unicode-able BibTeX to the modern Biblatex, with the Unicode-aware Biber.

If that isn't an option, use the Bibtexparser library. It contains code to convert from BibTeX-compliant TeX notation to Unicode, with bibtexparser.customization.convert_to_unicode. Building on the example given in the documentation:

import bibtexparser
with open(bibtex_file_name) as bibtex_file:
    parser = bibtexparser.bparser.BibTexParser()
    parser.customization = bibtexparser.customization.convert_to_unicode
    bibliography = bibtexparser.load(bibtex_file, parser=parser)
    for entry in bibliography.entries:
        if entry.has_key('file'):
            shutil.copy(entry['file'], …)
  • Yes! this did the work. I used the python you wrote. It is more powerful than what I need, but I will have a look what else can I do with it. In the same way, I had no idea about the existence of Biblatex. Thank you – manolius Apr 26 '15 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.