1

I am trying to print out to a file from python so that I can immediately copy and paste the output into WinEdit to make a tabular.

Example of current code is as follows:

sys.stdout = open('C://...', 'w')
for i in xrange(len(freq)):
    if freq[i] > 3725.90649 and freq[i] < 3725.94165:
        print '{:>2}  {:>1} {:>23} {:>1} {:>9} {:>12} {:>10}{:>3}{:>12}'.format(1, "&", "3725.90649 - 3725.94165", "&", freq[i], "$\rightarrow", Tline[i], J_i[i], ")$ \\ \hline")
sys.stdout.close()

I want to get the output:

1 & 3725.90649 - 3725.94165 & 3725.9194 $\rightarrow R_{+}^{-}(3.5 )$ \\ \hline

But instead I get this:

1 & 3725.90649 - 3725.94165 & 3725.9194 $ightarrow R_{+}^{-}(3.5 )$ \ \hline

What is going on in the code? How can I get the desired output?

SZ23
  • 11
  • 4

2 Answers2

0

You need to escape all your '\' characters with another slash like '\\'

>>> print '\right'
ight

>>> print '\\right'
\right

Or use raw strings (use a leading r)

>>> print r'\right'
\right
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • So I just need to add an extra '\'?And what do you mean by "escape"? – SZ23 Jan 08 '15 at 21:16
  • Some string sequences are treated as special characters, and you need to "escape" them to treat them as normal strings. You can read more about that in [the documentation](https://docs.python.org/2/reference/lexical_analysis.html#string-literals) – Cory Kramer Jan 08 '15 at 21:17
  • Note that in this example I showed, I am escaping the special character `'\r'` which is a carriage return. – Cory Kramer Jan 08 '15 at 21:20
0

You can use also use raw string literal by prepending your string with r

print (r"\\ \hline")
Community
  • 1
  • 1
Lastre
  • 1
  • 2