【问题标题】:basic for loop and write(line), joining strings基本 for 循环和写入(行),连接字符串
【发布时间】:2016-10-29 15:45:43
【问题描述】:

我正在为学校做作业。这是我第一次尝试使用 Python。我们应该将 Juliet 和 Romeo 复制到一个新文件中,但只有行为和场景指示,只保留 Juliet 和 Romeo 分区。这是我的两个问题:出于某种原因,有时它会复制场景的标题两次。第二个问题,每当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()

【问题讨论】:

  • 顺便说一句,在创建布尔变量时,您可以简化语句,只需编写scene = 'Scene' in lineJuliet = '>>' in line and 'Juliete' in line
  • 第一次出现重复行是因为当变量 act 和 scene 为 True 时,剩余变量 Juliet 和 Romeo 在之前的迭代中也可以为 True。
  • 另外,您不需要调用 readlines() 方法来遍历文件 - for line in orig: .... 就足够了。
  • 使用str.replace() 将对话框字符串的换行符替换为空格。
  • 我应该在哪里以及如何使用它?

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


【解决方案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()

【讨论】:

  • 试图理解这一点,非常感谢!但是“行为”和“场景”不是必须定义的吗?
  • 不,你可以随时补上。只需写 New_Variable = ...,然后 Python 就知道了。其他编程语言要求您声明新变量,但 Python 不需要。请随时要求澄清代码部分:)
  • 现在它根本不会将任何内容复制到新文件中
  • 修复了两行,请重新运行新版本。谢谢。
  • NameError: name 'act' is not defined
【解决方案2】:

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

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

【讨论】:

  • 感谢您的明确答复。你介意给我一些代码方面的指导吗?我是一个初学者,有点溺水了..
【解决方案3】:

跟进@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)

我们不知道您输入文件的格式或您所说的单行分区是什么意思,所以那里的其他信息会有所帮助。

【讨论】:

  • a) 所有的输入输出文件都是.txt
  • b) 分区我的意思是一个角色在将单词传递给其他人之前必须说的所有内容。这可以从一个句子到跨越几行的适当独白。
猜你喜欢
  • 2015-10-24
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
相关资源
最近更新 更多