删除部分I / O文件python

时间:2016-12-09 02:47:10

标签: python io

我正在尝试构建一个程序,用于记录I / O文件中学生的姓名和等级。问题是用户从remove函数中删除文件中的名称。谢谢你看看:))

(x是用户给出的名称)

    def remove(x):
    with open ("grades.txt", "rt") as f:
            text = f.read()

    if x in text:

            new = del text[x]
            with open ("grades.txt", "wt") as f:
                    text = f.write(new)

    else:
            print("name not in the file")

1 个答案:

答案 0 :(得分:0)

由于x和text是字符串,因此首先要在文本中找到x的起始索引。

<fieldset id="add-remove-buttons">
   <input class="button" name="commit" type="submit" value="add to cart">
   <a href="/shop" class="button continue">keep shopping</a>
</fieldset>

<script id="cart-controls-add" type="text/x-nano-tmpl">
  <form accept-charset="UTF-8" action="/shop/170325/add" class="add" data-remote="true" id="cart-addf" method="post">
    <div style="margin:0;padding:0;display:inline">
       <input name="utf8" type="hidden" value="&#x2713;" />
       <input name="authenticity_token" type="hidden" value="YzR1rCDbfkdbY9M7Fvlx61/adGM9rO51uo6kFFBtdnk=" /></div>
    <fieldset>
      <input id="size" name="size" type="hidden" value="40401" />
      <a href="/shop/accessories/ysw1o8v0k" class="next">next accessory &gt;</a>
    </fieldset>
    <fieldset id="add-remove-buttons">
       <input class="button" name="commit" type="submit" value="add to cart" />
       <a href="/shop" class="button continue">keep shopping</a>
    </fieldset>
  </form>
</script>

编辑: 根据您存储名称的方式,您可能还需要删除逗号或换行符。