如何使用 python 将下面的测试准确地写入文本文件?当我必须编写多个具有不同参数的文本文件时,这是必需的。
MY_FILE = E:\test.jpg
BAND_SUBSET = ( 1 0 0 )
SPATIAL_SUBSET1 = ( 25.0 50.0 )
SPATIAL_SUBSET2 = ( 25.0 50.0 )
PARA1 = (
0.0 0.0 0.0
0.0 0.0 0.0 )
END = END
如何使用 python 将下面的测试准确地写入文本文件?当我必须编写多个具有不同参数的文本文件时,这是必需的。
MY_FILE = E:\test.jpg
BAND_SUBSET = ( 1 0 0 )
SPATIAL_SUBSET1 = ( 25.0 50.0 )
SPATIAL_SUBSET2 = ( 25.0 50.0 )
PARA1 = (
0.0 0.0 0.0
0.0 0.0 0.0 )
END = END
with open('somefile.txt', 'w') as fp:
fp.write('''MY_FILE = E:\test.jpg
BAND_SUBSET = ( 1 0 0 )
SPATIAL_SUBSET1 = ( 25.0 50.0 )
SPATIAL_SUBSET2 = ( 25.0 50.0 )
PARA1 = (
0.0 0.0 0.0
0.0 0.0 0.0 )
END = END''')
with open('fileName', 'w') as f:
f.write(yourString)
在哪里
>>> yourString = """MY_FILE = E:\test.jpg
BAND_SUBSET = ( 1 0 0 )
SPATIAL_SUBSET1 = ( 25.0 50.0 )
SPATIAL_SUBSET2 = ( 25.0 50.0 )
PARA1 = (
0.0 0.0 0.0
0.0 0.0 0.0 )
END = END"""
您可以使用以下代码替换这些值。
with open('fileName', 'w') as f:
f.write(yourString.format(band_subset, spatial_subset_1, spatial_subset_2, para1))
在哪里
yourString = """MY_FILE = E:\test.jpg
BAND_SUBSET = {}
SPATIAL_SUBSET1 = {}
SPATIAL_SUBSET2 = {}
PARA1 = {}
END = END"""
始终使用该help
函数或查阅 Python 文档。
write(...)
write(str) -> None. Write string str to file.