简体   繁体   English

我正在尝试通过python程序读取文件的内容

[英]I am trying to read the content of a file through a python program

The problem is related to the text file path. 问题与文本文件路径有关。 As soon as i copy the text file in the directory of the python program and run it,it runs successfully. 一旦我将文本文件复制到python程序的目录中并运行它,它就会成功运行。 Here is the code-> 这是代码->

ana=open("C:\Users\HP\Downloads\practice1.txt","r")
pj=ana.read()
ana.close()
pj=pj.splitlines()
print(pj)
for i in pj:
    print(i)

The error displayed is-> 显示的错误是->

[unicode error] 'unicodeescape'  codec can't decode bytes in position 2-3:truncated\UXXXXXXXX escape

Please help! 请帮忙!

Try any of the below : 尝试以下任何一种方法:

ana=open(r'C:\Users\HP\Downloads\practice1.txt')
ana=open('C:\\Users\\HP\\Downloads\\practice1.txt')
ana=open('C:/Users/HP/Downloads/practice1.txt')

The backslash character (\\) has a special meaning when it appears in a string (or bytes) literal in Python. 当反斜杠字符(\\)出现在Python的字符串(或字节)文字中时,具有特殊含义。 Backslash is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. 反斜杠用于转义具有特殊含义的字符,例如换行符,反斜杠本身或引号字符。

ana=open("C:\Users\HP\Downloads\practice1.txt","r")

In your example, Python is trying to escape the U , H , D and p characters because they have backslashes in front of them! 在您的示例中,Python试图转义UHDp字符,因为它们前面有反斜杠!

Fortunately, Python provides us with a syntax that allows us to write strings containing literal backslash characters. 幸运的是,Python为我们提供了一种语法,该语法使我们能够编写包含文字反斜杠字符的字符串。 Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; 字符串和字节文字都可以选择以字母“ r”或“ R”为前缀; such strings are called raw strings and treat backslashes as literal characters. 这样的字符串称为原始字符串 ,并将反斜杠视为原义字符。

ana=open(r"C:\Users\HP\Downloads\practice1.txt","r")

Now the backslash characters are treated as normal characters, and no escaping is attempted. 现在,将反斜杠字符视为普通字符,并且不尝试转义。

You can read more about this in the Python reference documentation about literals https://docs.python.org/3/reference/lexical_analysis.html#literals 您可以在有关文字的Python参考文档中阅读有关此内容的更多信息https://docs.python.org/3/reference/lexical_analysis.html#literals

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试读取 pdf 文件并将其显示在 python - I am trying to read a pdf file and display it in python 我正在尝试制作文件删除程序 - I am trying to make a file delete program Python 说它找不到我要重命名的文件,即使它可以读取文件名 - Python says it can't find the file I am trying to rename even though it can read the file name 我正在尝试在python中读取url,但读取不完整 - I am trying to read in a url in python but it is giving an incomplete read 我正在尝试读取 python 中的 utf-8 文件并保留 BOM,但是当我执行 file.read 时会自动删除 BOM - I am trying to read a utf-8 file in python and keep BOM, but BOM is automatically deleted when i do file.read SQL 和 python 在我尝试读取文件时出现错误 - SQL with python is giving me error when I am trying to read in a file 我正在尝试从python文本文件中的列中读取单词,但收到错误:“列表”对象不可调用 - I am trying to read a word from a column in a text file in python but get the error: 'list' object is not callable 我正在尝试在 TextWrangler 应用程序中使用 Python 读取、附加和排序文件中的所有单词。 - I am trying to read, append and sort all words in a file using Python in TextWrangler application. 我正在尝试读取 a.txt 文件并从中创建两个新列表。 在 python - I am trying to read a .txt file and create two new lists out of it. In python 我正在尝试制作一个计算平均值,最大值和最小值的程序? 蟒蛇 - I am trying to make a program that calculates the average, maximum, and minimum? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM