我正在尝试为我的父母编写一个使用Python的快速簿记程序。我希望它如何工作如下。每次父母打开此程序时,我都希望它在excel文件中添加新行。问题在于,每次程序运行时,它都会覆盖自身。在下面,您会看到我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
namespace ProiectLicenta.Pages.Userlist
{
public class IndexModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
public IndexModel(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
public List<IdentityUser> UserList { get; set; }
public void OnGet()
{
UserList = _userManager.Users.ToList();
}
public async Task<ActionResult> OnPostDel(string id)
{
UserList = _userManager.Users.ToList();
var User = await _userManager.FindByIdAsync(id);
if (User != null)
{
IdentityResult result = await _userManager.DeleteAsync(User);
}
return RedirectToPage("Index");
}
}
}
答案 0 :(得分:3)
根据Python open的定义:
Character Meaning
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open for updating (reading and writing)
使用正确的模式。 'w'
始终以空文件开头。 'a'
是始终写入文件末尾的正确模式。
答案 1 :(得分:2)
尝试用“ a”代替“ w”,然后告知会发生什么。 “ w”仅用于写入,光标不会移到文件末尾,因此它会覆盖