| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 416 人关注过本帖
标题:新人求指导,请问这个python代码为什么运行不了。
只看楼主 加入收藏
huhoo123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-3-31
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
新人求指导,请问这个python代码为什么运行不了。
···
f = open('C:/Users/Administrator/Desktop/yingwen.txt',"r")
all_words = []
for s in f.readlines():
    s = s.replace(","," ").replace("."," ").lower()
    all_words.extend(s.split(" "))
all_words = list(set(all_words))
all_words.sort()

f2 = open('C:\Users\Administrator\Desktop\ying.txt','w')
f2.write("\n".join(all_words))
f2.close()
···

主要实现的功能是讲一本英文小说的文章全部采集成单次罗列到一个新的txt文章里。但是怎么运行都是错误。错误内容是:
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file      
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line
320, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2023.4.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line
294, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "c:\Users\Administrator\Desktop\胡\python\523.py", line 9
    f2 = open('C:\Users\Administrator\Desktop\ying.txt','w')
                                                       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
搜索更多相关主题的帖子: File line Users python lib 
2023-03-31 14:32
sheeboard
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:16
帖 子:79
专家分:442
注 册:2023-2-16
收藏
得分:20 
目录斜杠反了。f = open(r'C:\Users\Administrator\Desktop\yingwen.txt',"r")

[此贴子已经被作者于2023-3-31 14:46编辑过]

2023-03-31 14:43
huhoo123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-3-31
收藏
得分:0 
回复 2楼 sheeboard
解决了,谢谢
2023-04-01 10:27
快速回复:新人求指导,请问这个python代码为什么运行不了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020077 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved