Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'essentially': 0.04; 'languages.': 0.04; 'string.': 0.05; 'string': 0.09; 'character,': 0.09; 'escape': 0.09; 'forms,': 0.09; 'literal': 0.09; 'msg': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'changes': 0.15; '*string': 0.16; 'backslash': 0.16; 'backslash,': 0.16; 'dictionaries': 0.16; 'dig': 0.16; 'escapes': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'holy': 0.16; 'literal.': 0.16; 'literals': 0.16; 'literals*': 0.16; 'middle,': 0.16; 'restructured': 0.16; 'string:': 0.16; 'applies': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'file,': 0.19; 'passing': 0.19; "python's": 0.19; 'things.': 0.19; '>>>': 0.22; 'code,': 0.22; '(in': 0.22; 'handles': 0.22; 'putting': 0.22; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'apply.': 0.24; "aren't": 0.24; 'string,': 0.24; 'text,': 0.24; 'text.': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'second': 0.26; 'pass': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; "doesn't": 0.30; 'characters': 0.30; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'keys': 0.31; 'quotes': 0.31; 'file': 0.32; 'figure': 0.32; 'stuff': 0.32; 'another': 0.32; 'text': 0.33; 'trouble': 0.34; 'anywhere': 0.35; 'common': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'done': 0.36; 'so,': 0.37; 'two': 0.37; 'list': 0.37; 'does': 0.39; 'quote': 0.39; 'though,': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'dave': 0.60; 'most': 0.60; 'tell': 0.60; 'matter': 0.61; 'simply': 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; 'different': 0.65; 'finish': 0.65; 'mar': 0.68; 'combining': 0.68; '2015': 0.84; 'everything.': 0.84; 'or...': 0.84; 'world!"': 0.84; 'angel': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=4AKLLOMotlm9kyuMi/1Xpk48W8qlsIzcUhhXui99gEM=; b=VZUXBcXdtbwvQp1OUVf6LDx9TvJ6WnWxANKp8dal9Q4QFDsA0BnCVowKnfmAI16ZO3 x+XQv9dCJjfLd4chzvUlpk/YTy28pWE51mu5u97R4ocKChuKP/Sa3fD7ZrZFML7P77Wf cBB8Rk0bcHhxO4UYbyIhw8TLfWtTEPEOoQtQy59yJLPNEIx2apTyEaNAI2vk5BhtUcI2 FBZNYPz6yVmbxMPs/aiuRneWqZulUbDXQgTmzK1KeUtfmBKgsYnV6Xpw1pIgfT8ht9aU 4Hl+GCN7RRFwjfHJ1Y7oQx884qQVet+C2/Lm+P8NCNwMdBUNkmVWxlZygChJWW5sBZbd Ay4A== MIME-Version: 1.0 X-Received: by 10.50.62.110 with SMTP id x14mr24745760igr.2.1425336720075; Mon, 02 Mar 2015 14:52:00 -0800 (PST) In-Reply-To: References: <54f458a5$0$13003$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 3 Mar 2015 09:51:59 +1100 Subject: Re: rst and pypandoc From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425336728 news.xs4all.nl 2901 [2001:888:2000:d::a6]:39270 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86790 On Tue, Mar 3, 2015 at 9:30 AM, alb wrote: > Hi Dave, > > Dave Angel wrote: > [] >>> Rst escapes with "\", but unfortunately python also uses "\" for escaping! >> >> Only when the string is in a literal. If you've read it from a file, or >> built it by combining other strings, or... then the backslash is just >> another character to Python. > > Holy s***t! that is enlightning. I'm not going to ask why is that so, > but essentially this changes everything. Indeed I'm passing some strings > as literal (as my example), some others are simply read from a file > (well the file is read into a list of dictionaries and then I convert > one of those keys into latex). You have two different things happening here. The first is the concept of a "string literal", and the second is how pandoc handles things. Python's string literals come in a few different forms, but the most common is the one that looks the same as in several other languages. You start with a quote character, you put all your stuff in the middle, and you finish with another quote: "Hello, world!" Trouble is, this makes it really hard to put quotes into your string: "I said, "Hello, world!"" That's not going to work properly! So we need to tell Python that those interior quotes aren't the end of the string. That's done with a backslash: "I said, \"Hello, world!\"" And of course, that means you have to escape the backslash if you want to have one in the text. But all of this is just for putting *string literals* into your source code. If it's not Python source code, these rules don't apply. You can read a line of text from the user and it'll be unchanged: >>> msg = input("Enter a string: ") Enter a string: This is a string, but not a "string literal". >>> print(msg) This is a string, but not a "string literal". (in Python 2, use raw_input instead of input) Same applies to reading from a file, or anywhere else. If it's not Python source code, it doesn't matter what characters are in the string, they're all just characters. > unfortunately when I pass that to pypandoc, as if it was restructured > text, I get the following: > > In [36]: f = open('test.txt', 'r') > > In [37]: s = f.read() > > In [38]: print s > this is \some restructured text. > > > In [39]: print pypandoc.convert(s, 'latex', format='rst') > this is some restructured text. > > what happened to my backslash??? That's something you'll have to figure out with pypandoc. I don't know how it interprets the backslash, so you'll have to dig into its documentation. At least now, though, you can print out your string and see that it really does have its backslash in it. ChrisA