python中的文件到字符串转换

2024-09-29 21:21:37 发布

您现在位置:Python中文网/ 问答频道 /正文

dictionary = file . read()

我目前正在为2017年密码挑战赛创建密码解算器 我有一个5万8千字的word文档,但是我无法在python2.7.9中以字符串的形式获取该文件 我试过很多我在网上读过的东西,比如上面的代码,但都没有用。 我还需要这是很容易理解,因为我是新的python

谢谢!不要消极,要有建设性!你知道吗

这个词来自: http://www.mieliestronk.com/corncob_lowercase.txt


Tags: 文件字符串代码文档密码readdictionary形式
3条回答

我想你应该看看这个你想做什么(http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

这应该是有帮助的

你所要做的就是:

with open("dictionary.txt") as f: # Open the file and save it as "f"
    dictionary = f.read()         # Read the content of the file and save it to "dictionary"

如果您想从网站上阅读,请尝试以下方法:

import urllib2
dictionary = urllib2.urlopen("http://www.mieliestronk.com/corncob_lowercase.txt").read() # Open the website from the url and save its contents to "dictionary"

您可能应该参考web上的一些代码示例来读取文件。你需要这样的东西:

fp = open(fname, "r")
lines = fp.readlines()
for line in lines:
   do_something_with_the_lines
fp.close()

相关问题 更多 >

    热门问题