编辑.txt文件 - 算法不起作用

时间:2017-02-09 13:11:28

标签: python algorithm python-3.x

我想用代码自动编辑.txt文件。包含victory_poins的所有内容都将被删除,并在“history = {”语句后以另一种形式输入。但最终,它增加了一个额外的历史= {。为什么呢?

代码:

var result = _context.DwPropertyMasters.Where(x => x.ShowMapPoint == "Y")
                .Select(x => new
                {
                    x.LandId,
                    a = x.Development == null || x.Development == "" ? x.Location : x.Development,
                    x.MapPointX,
                    x.MapPointY,
                    AreaSize = x.AreaSize ?? 0,
                    Premium = x.Premium ?? 0,
                    b = (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize == 0 ? null : x.AreaSize) ?? 0,
                    c =
                    _context.DwPropertyDetails.Where(
                            z => (z.TransactionPrice > 0 || z.TransactionPrice != null) && z.LandId == x.LandId)
                        .GroupBy(z => z.LandId)
                        .Select(g =>
                            (g.Sum(p => p.TransactionPrice) == 0 ? null : g.Sum(p => p.TransactionPrice)) /
                            (g.Sum(p => p.ActualSize) == 0 ? null : g.Sum(p => p.ActualSize)) ?? 0)
                        .FirstOrDefault(),
                    d =
                    ((x.AreaSize2 == 0 ? null : x.AreaSize2) == 0
                        ? 0
                        : (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize2 == 0 ? null : x.AreaSize2)) ??
                    0,
                    x.LandType,
                    e =
                    _context.DwPropertyDetails.Where(
                            y => (y.TransactionPrice > 0 || y.TransactionPrice != null) && y.LandId == x.LandId)
                        .Select(y => new
                        {
                            a = 1
                        }).Count()
                });

输入:

def überschreiben(filename,vp, capital):
data_out=open(filename,"r")
data_in=open(filename+"_output.txt","w")

vpsegment=False
for line in data_out:
    if "\thistory" in line:
        data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')

    if "\t\tvictory_points" in line:
        vppivot=line
        vpsegment=True

    if vpsegment==True:
        if "}" in line:
            data_in.write("")
            vpsegment=False
        else:
            data_in.write("")
    else:
        data_in.write(line)
data_in.close()
data_out.close()

输出:

state={
id=1
name="STATE_1" # Corsica
manpower = 322900

state_category = town

history={
    owner = FRA
    victory_points = { 3838 1 }
    buildings = {
        infrastructure = 4
        industrial_complex = 1
        air_base = 1
        3838 = {
            naval_base = 3
        }
    }
    add_core_of = FRA
}

provinces={
    3838 9851 11804 
}
}

第二个历史= {来自哪里?

1 个答案:

答案 0 :(得分:1)

让我们看一下阅读history{行时会发生什么:

if "\thistory" in line:
    data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')

该行包含“\ thistory”,因此它会写入行(它会写出第一个“history {”)和其他内容

if "\t\tvictory_points" in line:
    vppivot=line
    vpsegment=True

没有任何反应,因为该行不包含“\ t \ tvictory_points”

if vpsegment==True:
    if "}" in line:
        data_in.write("")
        vpsegment=False
    else:
        data_in.write("")
else:
    data_in.write(line)

vpsegment==False所以它转到else语句并写下"\thistory{"