在Python列中打印

2024-09-25 00:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件(带制表符分隔),例如:

Plate   Well Group Type    Sample          Wavelength Reading Abs   Meas. Time [s]
Plate 1  A05 Assay Blank   Blank_Assay 1/1 340        1       0.113 0.080         
Plate 1  A05 Assay Blank   Blank_Assay 1/1 340        2       0.114 3.660         
Plate 1  A05 Assay Blank   Blank_Assay 1/1 340        3       0.114 7.230         
Plate 1  A01 Assay Control Ctrl_0001 1/1   340        1       0.706 0.000         
Plate 1  A01 Assay Control Ctrl_0001 1/1   340        2       0.706 3.580           
Plate 1  A01 Assay Control Ctrl_0001 1/1   340        54      0.685 189.740       
Plate 1  B01 Assay Control Ctrl_0002 1/1   340        4       0.698 11.220        

等等。在

我用\t分隔符将其拆分并访问所有单独的列。在

我想让它给我

^{pr2}$

我希望时间按时间顺序从0到>最高数字排序,在每个时间,将相应的吸光度读数添加到相应的列(带标题)。在

我希望最后的输出被输出到一个新的文件。在


Tags: sampletype时间groupcontrol制表符ctrlblank
2条回答

这对我有用:

import numpy as np
import collections

data = np.genfromtxt("values.csv",delimiter=' ',skip_header=1,dtype=None)

dataDict = {}
for i,dat in enumerate(data):
    dataDict[dat[10]]=[dat[2],dat[9]]

orderedDict = collections.OrderedDict(sorted(dataDict.items()))

fp = open("outResult.csv",'wb')
fp.write(" \tA05\tA01\tB01\n")
fp.write("Time(S)\tAbs\tAbs\tAbs\n")
print " \tA05\tA01\tB01"
print "Time(S)\tAbs\tAbs\tAbs"
for key in orderedDict.keys():
    str1 = ''
    str2 = ''
    str3 = ''
    if orderedDict[key][0] == 'A01':
        str1 = orderedDict[key][1]
        str2 = ' '
        str3 = ' '
    elif orderedDict[key][0] == 'A05':
        str1 = ' '
        str2 = orderedDict[key][1]
        str3 = ' '
    else:
        str1 = ' '
        str2 = ' '
        str3 = orderedDict[key][1]
    fp.write("%.02f\t%s\t%s\t%s\n"%(key,str1,str2,str3))
    print "%.02f\t%s\t%s\t%s"%(key,str1,str2,str3)
fp.close()

你说:

I want time Time(s) to be sorted in chronological order from 0 -> highest number and at each respective time, the corresponding absorbance reading to be added to the respective column (with the header).

查看您最初发布的完整数据(在被编辑截短之前),您将无法根据Meas. Time [s]列匹配abs读数,因为A05、A01和B01的每个读数都不相同。相反,(我想?)Reading数字列是连接各个读数的方法,因为Meas. Time [s]对于所有井的每个读数编号都是相似的。在

因此,使用Python csv模块,读取文件并按Reading列对数据进行分组。然后按顺序迭代分组读数,为每个Well获取Abs的值。分组是使用dicts的^{}完成的。在

import csv
from collections import defaultdict

# CSV column numbers
WELL_COL = 1
READING_COL = 6
ABS_COL = 7

with open('readings') as infile:
    data = defaultdict(dict)
    reader = csv.reader(infile, delimiter='\t')
    _ = next(reader)    # skip the header line
    for row in reader:
        data[int(row[READING_COL])][row[WELL_COL]] = row[ABS_COL]

outfile_fmt = '{:<10}{:<10}{:<10}{}\n'    # N.B. new line suitable for file.write(), not print()

with open('abs_readings', 'w') as outfile:
    outfile.write(outfile_fmt.format('', 'A05', 'A01', 'B01'))
    outfile.write(outfile_fmt.format('Reading', 'Abs', 'Abs', 'Abs'))
    for reading, abs in sorted(data.items()):
        outfile.write(outfile_fmt.format(reading, abs['A05'], abs['A01'], abs['B01']))

从CSV文件中读取数据后,data如下所示:

^{pr2}$

最后,遍历字典(按读数排序)并输出abs值。最终输出应如下所示:

          A05       A01       B01
Reading   Abs       Abs       Abs
1         0.113     0.706     0.698
2         0.114     0.706     0.698
3         0.114     0.705     0.698
4         0.114     0.705     0.698
5         0.114     0.705     0.698
6         0.114     0.705     0.698
7         0.114     0.704     0.697
8         0.114     0.703     0.697
9         0.114     0.703     0.696
10        0.114     0.702     0.696
11        0.114     0.702     0.696
.
.
.
59        0.114     0.684     0.679
60        0.114     0.683     0.678

相关问题 更多 >