如何从文件中读取数据并获得一致性输出

时间:2015-09-23 19:41:20

标签: python c++ python-3.x for-loop

我有代码将抛出给定字符串的输出。

inputdata = "HELLO HELLO HELLO BOBBY WHAT ARE YOU DOING"
myDict = {}
linenum = 0

for word in inputdata.split():
    if not word in myDict:
        myDict[word] = []

    myDict[word].append(linenum)


print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

输出将是

   Word            Frequency     
 ARE           : 1              
 BOBBY         : 1              
 DOING         : 1              
 HELLO         : 3              
 WHAT          : 1              
 YOU           : 1   

但是当我尝试用.txt文件替换字符串时,脚本会提示弹出窗口输入文本,而不是从.txt文件中读取数据。

f = open(raw_input("eng.txt"), "r")
myDict = {}
linenum = 0

for word in f.split():
    if not word in myDict:
        myDict[word] = []

    myDict[word].append(linenum)


print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

3 个答案:

答案 0 :(得分:1)

f是示例中的文件资源,而不是字符串。您需要阅读f。例如:How do I read a text file into a string variable in Python

执行以下操作以处理newline个字符:

with open ("data.txt", "r") as myfile:
    f=myfile.read().replace('\n', '')

此外,raw_input用于命令提示符,在这种情况下无效。

答案 1 :(得分:1)

由于您有

std::istringstream inputdata("HELLO HELLO HELLO BOBBY WHAT ARE YOU DOING");

std::string word;
std::map<std::string, size_t> counts;

while (inputdata >> word)
    ++counts[word];

for (auto const &w : counts)
    std::cout << std::setw(15) << w.first << ": " << w.second << "\n";

请注意,我已经跳过了生成/存储行号,因为您还没有使用它们。 : - )

答案 2 :(得分:1)

备注:

raw_input method用于从提示

获取用户输入

对您自己的代码进行更改

f = open("eng.txt", "r")
myDict = {}
linenum = 0

for word in f.read().split():
    if not word in myDict:
        myDict[word] = []
    myDict[word].append(linenum)

print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

使用字典

的集合default method简化代码

<强>代码1:

myDict = {}
linenum = 0
with open("eng.txt", "r") as f:
    for word in f.read().split():
        myDict.setdefault(word,[]).append(linenum)

print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

示例输入:

was very very afraid can you help
me
me
me

输出

Word            Frequency      
afraid         : 1              
can            : 1              
help           : 1              
me             : 3              
very           : 2              
was            : 1              
you            : 1