需要从另一个 txt 文件在现有 txt 文件中创建一个新列
Need to make a new column in an existing txt file from another txt file
我有一个包含 10 列和大约 5000 行数据的现有 txt 文件。 txt 文件(我们称之为 specfile)有 headers,我用它来使用 ascii.read() 访问数据点。我需要做的是添加三个名为 'UMINV'、'NUVMINJ' 和 'VMINJ' 的新列作为 headers,然后将它们的后续数据指向 specfile。所有这些列都在另一个文件 (colfile) 中。我尝试使用以下解决方案:How do i add a column to an existing text file in python? but the columns look like this:
我的代码看起来像
specpath = '/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp.dat'
colpathin = '/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat'
specfile = ascii.read(specpath)
colfile = ascii.read(colpathin)
UMINV = str(colfile['UMINV'])
with open('/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat') as ifh, open('/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp2.dat', 'w') as ofh:
for lineno, line in enumerate(ifh):
line = line.rstrip() # remove newline
color = UMINV[lineno % len(UMINV)] # choose color
line += ' ' + color # append color
ofh.write(line + '\n') # write line
我没有收到任何错误代码,但我得到的附加结果不正确,如图所示。任何直升机都会很棒!
我不确定 ascii.read()
函数 returns 的结构是什么样的。但是试试这个,将 str()
转换从 UMINV
分配行移动到 color
行;另外,添加一列 header,并将 UMINV
列上的 lineno
索引调整一:
specpath = '/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp.dat'
colpathin = '/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat'
specfile = ascii.read(specpath)
colfile = ascii.read(colpathin)
UMINV = colfile['UMINV']
with open('/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat') as ifh, open('/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp2.dat', 'w') as ofh:
for lineno, line in enumerate(ifh):
line = line.rstrip() # remove newline
color = str(UMINV[lineno-1 % len(UMINV)]) # choose color
if lineno == 0:
line += ' UMINV' # append header
else:
line += ' ' + color # append color
ofh.write(line + '\n') # write line
我有一个包含 10 列和大约 5000 行数据的现有 txt 文件。 txt 文件(我们称之为 specfile)有 headers,我用它来使用 ascii.read() 访问数据点。我需要做的是添加三个名为 'UMINV'、'NUVMINJ' 和 'VMINJ' 的新列作为 headers,然后将它们的后续数据指向 specfile。所有这些列都在另一个文件 (colfile) 中。我尝试使用以下解决方案:How do i add a column to an existing text file in python? but the columns look like this:
我的代码看起来像
specpath = '/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp.dat'
colpathin = '/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat'
specfile = ascii.read(specpath)
colfile = ascii.read(colpathin)
UMINV = str(colfile['UMINV'])
with open('/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat') as ifh, open('/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp2.dat', 'w') as ofh:
for lineno, line in enumerate(ifh):
line = line.rstrip() # remove newline
color = UMINV[lineno % len(UMINV)] # choose color
line += ' ' + color # append color
ofh.write(line + '\n') # write line
我没有收到任何错误代码,但我得到的附加结果不正确,如图所示。任何直升机都会很棒!
我不确定 ascii.read()
函数 returns 的结构是什么样的。但是试试这个,将 str()
转换从 UMINV
分配行移动到 color
行;另外,添加一列 header,并将 UMINV
列上的 lineno
索引调整一:
specpath = '/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp.dat'
colpathin = '/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat'
specfile = ascii.read(specpath)
colfile = ascii.read(colpathin)
UMINV = colfile['UMINV']
with open('/home/user/GOGREEN_TEMP/RESTFRAME_COLOURS/RESTFRAME_MASTER_SpARCS-0035_indivredshifts_temp.cat') as ifh, open('/home/user/GOGREEN_TEMP/SPECZ_MATCHED/compilation_SpARCS-0035_temp2.dat', 'w') as ofh:
for lineno, line in enumerate(ifh):
line = line.rstrip() # remove newline
color = str(UMINV[lineno-1 % len(UMINV)]) # choose color
if lineno == 0:
line += ' UMINV' # append header
else:
line += ' ' + color # append color
ofh.write(line + '\n') # write line