Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取地图文件_Python_Python 2.7_Parsing_File Io - Fatal编程技术网

Python 读取地图文件

Python 读取地图文件,python,python-2.7,parsing,file-io,Python,Python 2.7,Parsing,File Io,我正在读取地图文件,并将数据以列格式写入文本文件。下面是我写的代码,但它不适合我 fo = open(filename, "r+") fu = open("map.txt","w+") for line in fo: if (".data.g" in line): fu.write(line) print line, fo.close() fu.close() #to remove unwanted data f = open("map1.txt",

我正在读取地图文件,并将数据以列格式写入文本文件。下面是我写的代码,但它不适合我

fo = open(filename, "r+")
fu = open("map.txt","w+")

for line in fo:
    if (".data.g" in line):
        fu.write(line)
        print line,

fo.close()
fu.close()

#to remove unwanted data
f = open("map1.txt", "w+")
g = open("map.txt", "r+")

for line in g:
    if((".data " in line) and (".bss" in line) and(".text" in line )):
        break
    else:
        f.write(line)
        f.write('\n')

f.close()
g.close()
预期产量为

    2007c04c .data  g_isr_array
    2007c004 .data  g_retry_count
    2007c000 .data  g_pkt_hist_wptr
输入映射文件的格式为

.data          0x2007c000        0x0 G:/SJSUDev_V2/SJSU_Dev/projects/lpc1758_freertos_v2/_build/L4_IO/wireless/src/mesh.o
 .data.g_pkt_hist_wptr
                0x2007c000        0x4 G:/SJSUDev_V2/SJSU_Dev/projects/lpc1758_freertos_v2/_build/L4_IO/wireless/src/mesh.o
 .data.g_retry_count
                0x2007c004        0x1 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/wireless/src/mesh.o
 .data.g_our_node_id
                0x2007c005        0x1 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/wireless/src/mesh.o
 .data          0x2007c006        0x0 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/wireless/src/nrf24L01Plus.o
 .data          0x2007c006        0x0 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/wireless/src/wireless.o
 .data          0x2007c006        0x0 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/src/gpio.o
 .data          0x2007c006        0x0 G:/SJSUDev_V2/SJSU_Dev/projects/ lpc1758_freertos_v2/_build/L4_IO/src/io_source.o
我只想读取以“.data.g”开头的全局变量,但地址在下一行,我的代码无法读取

以下是一些步骤:

  • 使用“with”打开文件
  • 对于每一行,扫描
    行[0:7]
    ,并与
    '.data.g'
    进行比较,以确定它是否是您想要的行。如果是,请使用line.split()拆分该行,并在.data之后提取零件。把绳子切成薄片。要在下一行中获得地址,您需要向前移动一行。有关如何操作,请参见此链接:
  • 一旦你有了那一行,做一个字符串分割和切片得到地址。现在使用所有三个数据项,写入文件

    对于
    行[0:7]='.data.g'
    条件中的任何行,请向前跳过

    例如:

    with open('map.txt', 'r') as readF:
      with open('map1.txt', 'w') as writeF:
        for line in readF: 
           if line.startswith('.data.g'):
              try:         # next() can cause StopError, gotta catch it
                row = line.split()
                part_after_data = row[0][6:]
                line = next(ireadF)
                addr = line.split()[0]
                writeF.writeline(addr + ' .data '+ part_after_data )
              except:
                pass
    
    以下是一些步骤:

  • 使用“with”打开文件
  • 对于每一行,扫描
    行[0:7]
    ,并与
    '.data.g'
    进行比较,以确定它是否是您想要的行。如果是,请使用line.split()拆分该行,并在.data之后提取零件。把绳子切成薄片。要在下一行中获得地址,您需要向前移动一行。有关如何操作,请参见此链接:
  • 一旦你有了那一行,做一个字符串分割和切片得到地址。现在使用所有三个数据项,写入文件

    对于
    行[0:7]='.data.g'
    条件中的任何行,请向前跳过

    例如:

    with open('map.txt', 'r') as readF:
      with open('map1.txt', 'w') as writeF:
        for line in readF: 
           if line.startswith('.data.g'):
              try:         # next() can cause StopError, gotta catch it
                row = line.split()
                part_after_data = row[0][6:]
                line = next(ireadF)
                addr = line.split()[0]
                writeF.writeline(addr + ' .data '+ part_after_data )
              except:
                pass
    

    如果数据是以发布的格式和顺序显示的,您只需要不以
    开头的第一行。data

    from itertools import imap
    
    with open("map.txt") as f:
        rows = imap(str.split, f)
        for row in rows:
            if row[0] != ".data":
                a, b = row[0].rsplit(".", 1)
                print(next(rows)[0],a, b)
    
    使用您的输入将为您提供:

    ('0x2007c000', '.data', 'g_pkt_hist_wptr')
    ('0x2007c004', '.data', 'g_retry_count')
    ('0x2007c005', '.data', 'g_our_node_id')
    
    您也可以使用以
    .data.开头的行。

    from itertools import imap
    
    with open("map.txt") as f:
        rows = imap(str.split, f)
        for row in rows:
            if row[0].startswith(".data."):
                a, b = row[0].rsplit(".", 1)
                print(next(rows)[0],a, b)
    

    这将给您相同的结果。

    如果数据的格式和发布顺序相同,您只需要不以
    开头的第一行。data

    from itertools import imap
    
    with open("map.txt") as f:
        rows = imap(str.split, f)
        for row in rows:
            if row[0] != ".data":
                a, b = row[0].rsplit(".", 1)
                print(next(rows)[0],a, b)
    
    使用您的输入将为您提供:

    ('0x2007c000', '.data', 'g_pkt_hist_wptr')
    ('0x2007c004', '.data', 'g_retry_count')
    ('0x2007c005', '.data', 'g_our_node_id')
    
    您也可以使用以
    .data.开头的行。

    from itertools import imap
    
    with open("map.txt") as f:
        rows = imap(str.split, f)
        for row in rows:
            if row[0].startswith(".data."):
                a, b = row[0].rsplit(".", 1)
                print(next(rows)[0],a, b)
    

    这将给您相同的结果。

    当您找到感兴趣的.data.g行时,需要扫描下一行。您可以使用iter()和next()执行此操作。查看我的答案。当您找到感兴趣的.data.g行时,需要扫描下一行。您可以使用iter()和next()执行此操作。参见我的答案。仅供参考,不需要转换步骤
    ireadF=iter(readF)
    ;类似文件的对象是真正的迭代器,而不是iterables;用
    iter()
    包装是无害的(迭代器协议要求
    iter(迭代器)
    返回原始迭代器),但没有必要。另外,我将使用
    if line.startswith('.data.g'):
    而不是显式的切片和相等性检查;显式切片索引是危险的,因为目标字符串中的更改需要更改切片索引以匹配,否则每次测试都会失败。代码维护期间容易出错。仅供参考,不需要转换步骤
    ireadF=iter(readF)
    ;类似文件的对象是真正的迭代器,而不是iterables;用
    iter()
    包装是无害的(迭代器协议要求
    iter(迭代器)
    返回原始迭代器),但没有必要。另外,我将使用
    if line.startswith('.data.g'):
    而不是显式的切片和相等性检查;显式切片索引是危险的,因为目标字符串中的更改需要更改切片索引以匹配,否则每次测试都会失败。在代码维护过程中容易出错。