有关从文件中读取总字数的疑问

时间:2014-11-21 20:55:10

标签: python

我正在尝试从目录中读取文件并将每个文件中的第一个句子写入一个新文件,直到100个单词(或者说我想写完整句子的单词超过100个)被写入新文件。< / p>

我的做法如下:

f = open(file1.txt, "w")
f.close()
for d_file in os.listdir(path):
    d_file_path = os.path.join(path, d_file)
    if os.path.isfile(d_file_path):
        with open(d_file_path, "r") as f:
            first = f.readline()
            f1 = open ("file1.txt", "r")
            textInput = f1.read()
            f1.close()
            l = len(textInput.split(' '))
            print l
            if l >= 0 and l <= 100:
                f2 = open("file1.txt", "a")
                f2.write(first)
                print first

但是,我的print语句输出错误,即使它正确写入新文件。

我的问题是: 为什么我将“l”的值设为0两次? 此外,当我在写入文件后,我只是找到文件中的总字数:

>>>f = open(file1.txt, 'r')
>>> text = f.read()
>>> l = len(text.split(' '))
>>> print l

我得到:111

但是,该文件是:

An influential lawmaker from the governing Labor Party on Saturday backed Spanish requests to question former Chilean dictator Gen. Augusto Pinochet, in London for back surgery, on allegations of genocide and terrorism.
British police said Saturday they have arrested former Chilean dictator Gen. Augusto Pinochet on allegations of murdering Spanish citizens during his years in power.
Eight years after his turbulent regime ended, former Chilean strongman Gen. Augusto Pinochet is being called to account by Spanish authorities for the deaths, detention and torture of political opponents.
Former Chilean dictator Gen. Augusto Pinochet has been arrested by British police on a Spanish extradition warrant, despite protests from Chile that he is entitled to diplomatic immunity.

不是有114个字吗?

有人可以回答我的问题吗?

编辑:

现在我正在做:l = len(textInput.strip().split())它给了我114个单词作为计数,但是那个print语句仍然是相同的。现在输出如下:

0
An influential lawmaker from the governing Labor Party on Saturday backed Spanish requests to question former Chilean dictator Gen. Augusto Pinochet, in London for back surgery, on allegations of genocide and terrorism.

0
British police said Saturday they have arrested former Chilean dictator Gen. Augusto Pinochet on allegations of murdering Spanish citizens during his years in power.

32
Eight years after his turbulent regime ended, former Chilean strongman Gen. Augusto Pinochet is being called to account by Spanish authorities for the deaths, detention and torture of political opponents.

56
Former Chilean dictator Gen. Augusto Pinochet has been arrested by British police on a Spanish extradition warrant, despite protests from Chile that he is entitled to diplomatic immunity.

86
President Fidel Castro said Sunday he disagreed with the arrest in London of former Chilean dictator Augusto Pinochet, calling it a case of international meddling.

114
114
114
114
114

1 个答案:

答案 0 :(得分:0)

你说的有114个单词。你怎么分裂&#39; &#39 ;.换行不算作&#39; &#39 ;.所以在第一行恐怖主义的最后一句话。&#39;并且下一行中的第一个单词“英国人”被视为恐怖主义格式中的一个单词。\ n英语&#39;。两条线也是一样的。因此,总共三个单词与前一个句子中的最后一个单词相结合,可以减少三个单词。

如果你想分割空格和新行,只需使用不带参数的split(),它应该有114个单词。以下详细信息来自文档here

str.split([sep[, maxsplit]])
  

如果未指定sep或为None,则使用不同的拆分算法   applied:连续空格的运行被视为单个   分隔符,结果将在开始时不包含空字符串   或者如果字符串具有前导或尾随空格则结束。所以,   拆分空字符串或只包含空格的字符串   使用无分隔符返回[]。

对于您正在打印的另一个问题,请准确提升您的代码。在我看来,你错过了关于file1.txt的引用,你可能也会遗漏其他东西。