循环和写入(行)的基本,连接字符串

时间:2016-10-29 15:45:43

标签: python string python-3.x for-loop

我正在为学校工作。这是我第一次尝试使用Python。我们应该将朱丽叶和罗密欧复制到一个新文件中,但只有行为和场景指示,只保留朱丽叶和罗密欧的分区。这是我的两个问题:由于某种原因,有时它会复制场景的标题两次。第二个问题,每当J或R说话时,分区必须缩小到一行。似乎无法找到解决问题的方法。 任何帮助将不胜感激。

my clean version, duplicity of indication

# opening files
orig = open('RomeoAndJuliet_clean.txt')
nohead = open('RomeoAndJuliet_cleanNoHeader.txt', 'w')
clean = open('theLovers.txt', 'w')

# declaring booleans
doIHaveToCopyTheLine=False
Romeo = False
Juliet = False
act = False
scene = False

# dissecting the play ##################################
orig = open('RomeoAndJuliet_clean.txt')
# creating the loop
for line in orig.readlines():
    # conserving the announcement of acts and scenes
    if 'ACT ' in line: # added a space so it doesn't copy a part of the    header "CONTRACT"
        act = True
    else:
        act = False       
    if 'Scene' in line:
        scene = True
    else:
        scene = False
    # excluding the other characters of the play
    if '>>' in line:
        if 'Romeo' in line:
            Romeo = True
        else:
            Romeo = False

        if 'Juliet' in line:
            Juliet = True
        else:
            Juliet = False
    # assigning functions to the booleans               
    if Juliet:
        clean.write(line)

    if Romeo:
        clean.write(line)

    if scene:
        clean.write(line+'\n')

    if act:
        clean.write(line+'\n')


nohead.close()       
clean.close()
orig.close()

3 个答案:

答案 0 :(得分:1)

您可以将条件与or结合使用。这应该消除重复。然后,你可能想要累积罗密欧和朱莉娅的话语,直到他们都不再说任何话,然后将收集的话语写到文件中:

# opening files
orig = open('RomeoAndJuliet_clean.txt')
nohead = open('RomeoAndJuliet_cleanNoHeader.txt', 'w')
clean = open('theLovers.txt', 'w')


# dissecting the play ##################################
orig = open('RomeoAndJuliet_clean.txt')
act = False
scene = False
Romeo = False
Juliet = False
# creating the loop

condensed = ''    # This will accumulate the utterances of R and J until    neither of them speaks anymore

for line in orig.readlines():
    act == 'ACT ' in line           # added a space so it doesn't copy a part of the    header "CONTRACT"       
    scene == 'Scene' in line

    if act or scene:
        clean.write(line+'\n')
        if condensed != '':
            clean.write(condensed)

    elif '>>' in line:
        Romeo == 'Romeo' in line
        Juliet == 'Juliet' in line
        if Romeo or Juliet:
            condensed += line + "\n"        # Add to the lines of R and J
        elif condensed != '':             # (that is, if there is anything at all to flush...)
            clean.write(condensed)
            condensed = ''              # Prepare for new utterances


if condensed != '':                         # Maybe there are left-over utterances?
    clean.write(condensed)

nohead.close()       
clean.close()
orig.close()

答案 1 :(得分:0)

我的假设是场景标题被复制两次,因为其中有朱丽叶或罗密欧。

对于第二个问题,您应该使用临时字符串并追加该字符对其的多行。当您到达空行时,您应该在文件中写入行并将临时字符串重置为空字符串。

答案 2 :(得分:0)

跟进@ Fejs的评论,为防止重复,请使用else / elif:

if scene:
    clean.write(line+'\n')
elif act:
    clean.write(line+'\n')    
elif Juliet:
    clean.write(line)
elif Romeo:
    clean.write(line)

我们不是你的输入文件的格式或者你在一行上的分区是什么意思,所以那里的附加信息会有所帮助。