2013-03-24 64 views

回答

72

\t是制表符。使用原始字符串代替:

test_file=open(r'c:\Python27\test.txt','r') 

或两倍的斜线:

test_file=open('c:\\Python27\\test.txt','r') 

或改用正斜线:

test_file=open('c:/Python27/test.txt','r') 
+1

非常感谢,我错过了\ t制表符 – 2013-03-24 12:06:56

+0

使用'/'工作。 – YumYumYum 2016-07-14 07:35:39

0

\t字符串中的标记一个制表符字符的转义序列。对于文字\,请使用\\

2

\在Python转义字符。将\t解释为标签。如果您需要字符串中的\字符,则必须使用\\

你的代码应该是:
test_file=open('c:\\Python27\\test.txt','r')

2

总是用“R”来获得原始字符串时要避免逃逸。

test_file=open(r'c:\Python27\test.txt','r') 
+0

有没有办法将原始修饰符预先添加到字符串中? – GreySage 2017-03-30 22:50:41

相关问题