Python从定义的变量读取文件

Python从定义的变量读取文件,python,file,input,Python,File,Input,我想让用户在Python中输入要读取的文件名(例如:text.txt),但它读取的是字符串而不是文件类型 r=(input("insert the name of the file")) File= open(r,'r') data=File.read() data.split() print(data) 新热 编辑:根据我答案上的评论,OP希望为文件中的所有单词(空格分隔)构建一个包含{word:wordcount}的dict 有一种非常好的方法可以做到这一点,但它并没有真正

我想让用户在Python中
输入要读取的文件名(例如:
text.txt
),但它读取的是字符串而不是文件类型

r=(input("insert the name of the file"))
  File= open(r,'r')
  data=File.read()
  data.split()
  print(data)
新热 编辑:根据我答案上的评论,OP希望为文件中的所有单词(空格分隔)构建一个包含
{word:wordcount}
dict

有一种非常好的方法可以做到这一点,但它并没有真正教会你什么,所以我将首先向你展示一种缓慢的方法,然后包括最佳解决方案

wordcountdict = dict()

r = input("filename: ")
with open(r, 'r') as infile:
    for line in infile:
        for word in infile.split(): # split on whitespace
            try:
                wordcountdict[word.lower()] += 1
                # try adding one to the word in the counter
            except KeyError:
                wordcountdict[word.lower()] = 1
                # If the word isn't in the dict already, set it to 1
现在,您可能想过滤掉一些常见的单词(
“at”
“I”
“then”
等),在这种情况下,您可以构建它们的黑名单(类似于
黑名单=['at',I',then']
)并在黑名单中执行
if word.lower():在
中继续
for word in infire.split()
和在
尝试/except
块之前。这将测试单词是否在黑名单中,如果在黑名单中,则跳过其余的执行

现在我向您承诺了一个很好的方法来实现这一点,那就是使用
collections.Counter
。它是专门为计算列表中的元素而创建的字典。有更快的方法计算单词,但Python(imo)中没有更干净的方法。你可以在网上查看时间安排

如果您从未使用过从
集合导入
,或
映射
函数,那么这将是非常神秘的,这就是为什么我没有将其放在第一位的原因!:)

基本上:
collections.Counter
将iterable作为参数,并计算iterable中的所有元素(因此` Counter([1,1,2,3,4,4])=={1:2,2:1,3:1,4:3})。您可以添加它们,它会在它们唯一的地方创建新键,并在它们不唯一的地方添加值

map(callable,iterable)
使用iterable的每个元素的参数运行
callable
,并返回一个
map
对象(在Python2中它是一个
list
),该对象本身是iterable(因此
map(str.lower,[“ThIS”,“Has”,“UppEr”,“and”,“LOWERcase”]))
为您提供了一个映射对象,您可以通过该对象进行迭代以获得
[“this”、“has”、“upper”、“and”、“lowercase”]
,因为所有对象都调用了
str.lower

当我们将二者结合在一起时,我们将输入
集合.Counter
一个
映射
对象,该对象将
行中的每个单词都小写。split()
,然后将其添加到一个最初为空的
计数器
,用作累加器。卡皮斯

老朽 现在还不清楚您的代码有什么问题,所以我将为您提供一些知识,并希望有一些东西能够坚持下去

r = input("insert the name of the file")
# this will be a string from the user, containing the file name, e.g.
# r == "text.txt"
# this is normal, because you pass `open` a filename, not a file object

File = open(r, "r")
# this makes File a file object that's pointed at the file name given from
# the user, opened for reading.

data = File.read()
# this sets data equal to the string containing the entire text in File
# This is usually NOT what you want to do, but without further explanation,
# I'll leave it be

data.split()
# this isn't an in-place operation, so you built a list out of the string
# data, split on newlines, then threw it away since you didn't assign it to
# anything.

print(data)
# prints your original data variable, because remember data.split() is not
# in-place, you'd have to do data = data.split(), but that's the wrong way
# to do that anyway....
这是我认为你想要做的

filename = input("insert the name of the file: ")
with open(filename, "r") as infile:
    data = infile.readlines()
这将使用上下文管理器(
with
),而不是
File=open(filename)
,因为这是一种更好的做法。它基本上使您不必在完成后键入
File.close()
,还可以解释在处理文件时可能出现问题的事实,因此,如果出于任何原因,您的代码引发异常而无法访问
文件.close()
,一旦文件对象离开
with
块,它仍然会关闭该文件对象

它还使用
.readlines()
而不是
.read().split()
,这实际上是一回事。这可能仍然不是您想要做的(在大多数情况下,您只想迭代一个文件,而不是将其所有数据转储到内存中),但是如果没有更多的上下文,我无法进一步帮助您

它还遵循PEP8的命名约定,
capitaledName
是类
File
不是一个类,它是一个文件对象,因此我将其命名为
infle
。我通常对文件名使用
in_
out
,但不使用YMMV

如果您对该文件的操作进行评论,我可以为您编写一些特定的代码。

New hotness 编辑:根据我答案上的评论,OP希望为文件中的所有单词(空格分隔)构建一个包含
{word:wordcount}
dict

有一种非常好的方法可以做到这一点,但它并没有真正教会你什么,所以我将首先向你展示一种缓慢的方法,然后包括最佳解决方案

wordcountdict = dict()

r = input("filename: ")
with open(r, 'r') as infile:
    for line in infile:
        for word in infile.split(): # split on whitespace
            try:
                wordcountdict[word.lower()] += 1
                # try adding one to the word in the counter
            except KeyError:
                wordcountdict[word.lower()] = 1
                # If the word isn't in the dict already, set it to 1
现在,您可能想过滤掉一些常见的单词(
“at”
“I”
“then”
等),在这种情况下,您可以构建它们的黑名单(类似于
黑名单=['at',I',then']
)并在黑名单中执行
if word.lower():在
中继续
for word in infire.split()
和在
尝试/except
块之前。这将测试单词是否在黑名单中,如果在黑名单中,则跳过其余的执行

现在我向您承诺了一个很好的方法来实现这一点,那就是使用
collections.Counter
。它是专门为计算列表中的元素而创建的字典。有更快的方法计算单词,但Python(imo)中没有更干净的方法。你可以在网上查看时间安排

如果您从未使用过从
集合导入
,或
映射
函数,那么这将是非常神秘的,这就是为什么我没有将其放在第一位的原因!:)

基本上:
collections.Counter
将iterable作为参数,并计算iterable中的所有元素(因此` Counter([1,1,2,3,4,4])=={1:2,2:1,3:1,4:3})。您可以添加它们,它会在它们唯一的地方创建新键,并在它们不唯一的地方添加值

map(callable,iterable)
使用iterable的每个元素的参数运行
callable
,并返回一个
map
对象(在Python2中它是一个
list
),该对象本身是iterable(因此
map(str.lower,[“ThIS”,“Has”,“UppEr”,“and”,“LOWERcase”]))
为您提供了一个映射对象,您可以通过该对象进行迭代以获得
[“this”、“has”、“upper”、“and”、“lowercase”]
,因为所有对象都调用了
str.lower

当我们把两者结合起来时,我们就是在喂食
r = raw_input('type the name of the file: ')
with open(r,'r') as myfile:
    for data in myfile:
        print(data.split())