首页 > 解决方案 > 在文本文件的行中查找位置

问题描述

我试图在某个字符串的文件中找到位置。我知道如何找到行,但我需要找到字符串行中的确切位置。

    curzt2 = open("C:\\Users\\User\\Desktop\\world\\program\\maps\\default\\world"+str(i)+".ZT2", "r+")
    content = curzt2.read()
    #find the line in the file
    curzt2line = content.index('terrFile="terrain.dat"')
    curzt2.seek(curzt2line, 0)
    curzt2.write(str(i))
    curzt2.close()

例如在这个文件中我想读到这一行

<ZTWorldMgr version="0" environment="world/environments/env1wetlands_medium.xml" tankenv="" terrFile="terrain.dat" camPitch="-0.000000" camYaw="-2.233864" camRoll="-0.000000" loadCount="1453" safehandledobjid="3">

并将terrain.dat 更改为terrai+str(i)+.dat

标签: python

解决方案


推荐阅读