标签:变化 error asd 解决办法 int bsp -- str dfa
部分转自:https://www.cnblogs.com/hellofengying/p/10183057.html
今天再打开文件名时,出现了错误,如下:
In [4]: path=‘D:\Code\PythonWorkPlace\PythonProject\pydata-book-2nd-edition\data sets\bitly_usagov\example.txt‘ In [5]: open(path).readline() --------------------------------------------------------------------------- IOError Traceback (most recent call last) C:\Users\Administrator\<ipython-input-5-bcaecf00da5d> in <module>() ----> 1 open(path).readline() IOError: [Errno 22] invalid mode (‘r‘) or filename: ‘D:\\Code\\PythonWorkPlace\\ PythonProject\\pydata-book-2nd-edition\\datasets\x08itly_usagov\\example.txt‘
不仔细观察还真注意不到,文件名发生了变化(如红色字体所示),也就是python字符串发生了转义。
解决办法:
1.字符串常量,则在字符串前加r
str=r“adfdfasd\tfdsadf\t”
print(str)
结果为:adfdfasd\tfdsadf\t
2.如字符串是从文件中读取,则在读取时加r
例如:有文件 test.txt中有 adfdfasd\tfdsadf\t E:\codes
则代码可以这么写:
file1=open(r"text.txt","r")
for line in file1:
print(line)
结果为:
adfdfasd\tfdsadf\t E:\codes
标签:变化 error asd 解决办法 int bsp -- str dfa
原文地址:https://www.cnblogs.com/sggggr/p/11874902.html