程序输出的行数比它应该的多

2024-10-06 11:25:18 发布

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

我一直在尝试写一个程序,读取行,每行它应该返回一个字母。不幸的是,问题在于输出中的行数比输入中的行数多(160k行输入中约有20行)

如果有人能告诉我我做错了什么,我会非常高兴的

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import Counter
from re import split
import re
from itertools import islice
import random

def checkisin(femalewords, malewords, testin):
    with open(femalewords) as filein:
        femalewordslist = filein.readlines()
    with open(malewords) as filein:
        malewordslist = filein.readlines()

    letters = "FM"

    with open(testin, "rU") as filein:
        for line in filein:
            malecounter = 0
            femalecounter = 0
            linia = line.rstrip()
            if any(word in linia for word in femalewordslist):
                femalecounter = femalecounter+1
            if any(word in linia for word in malewordslist):
                malecounter = malecounter+1
            if malecounter > femalecounter:
                print "M"
            elif malecounter < femalecounter:
                print "F"
            elif malecounter == femalecounter:
                print random.choice(letters)

checkisin("femaletopwords.txt", "maletopwords.txt", "in2.tsv")

Tags: infromimportreforifaswith
1条回答
网友
1楼 · 发布于 2024-10-06 11:25:18

正如用户tdelaney所说: 您使用的是python的通用换行符模式“rU”,它可能会生成与其他程序不同的行数,尤其是在文件中有一个未附加的“\r”时。完成for循环后,执行print repr(filein.newlines)。如果有问题,那就是您的问题。”

它解决了这个问题

相关问题 更多 >