关于从fi中读取总字数的疑问

2024-07-04 06:05:46 发布

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

我试图从一个目录中读取文件,并将每个文件中的第一句话写入一个新文件,直到新文件中写入100个单词(或略多于100个,因为我想写完整的句子)

我是这样做的:

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

Tags: 文件oftopathinforonopen
1条回答
网友
1楼 · 发布于 2024-07-04 06:05:46

你说的有114个字。你怎么会在''上分裂。换行符不算作“”。所以第一行中的最后一个词“恐怖主义”和下一行中的第一个词“英国人”按“恐怖主义”的格式计算。另外两条线也是一样。所以总共有三个单词和前一句的最后一个单词连在一起,就少了三个单词

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

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

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

对于您的另一个问题是什么正在打印,请升级您的代码与您所拥有的。在我看来,file1.txt中缺少了一个引号,而且还可能缺少其他内容

相关问题 更多 >

    热门问题