簡體   English   中英

在列Python中打印

[英]Printing in columns 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分隔符拆分它並訪問所有單獨的列。

我希望它能給我

               A05               A01        B01
Time(S)        Abs               Abs        Abs
0

我希望時間(0)按時間順序從0 - >最高數字排序,並且在每個相應時間,相應的吸光度讀數將被添加到相應的列(帶有標題)。

我希望將最終輸出輸出到新文件。

這對我有用:

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()

你說:

我希望時間(0)按時間順序從0 - >最高數字排序,並且在每個相應時間,相應的吸光度讀數將被添加到相應的列(帶有標題)。

查看您最初使用問題發布的完整數據(在編輯器截斷之前),您將無法根據Meas. Time [s] abs讀數Meas. Time [s] Meas. Time [s]列,因為A05,A01和B01的每個讀數都不相同。 相反,(我認為?) Reading數字列是加入相應讀數的方法因為Meas. Time [s] 對於所有井,每個讀數的Meas. Time [s]是相似的。

因此,使用Python csv模塊,讀取文件並按Reading列對數據進行分組。 然后,在分組讀數迭代,從而,抓住價值Abs每個Well 使用dicts的collections.defaultdict完成分組。

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如下所示:

>>> from pprint import pprint
>>> pprint(data.items())
[(1, {'A01': '0.706', 'A05': '0.113', 'B01': '0.698'}),
 (2, {'A01': '0.706', 'A05': '0.114', 'B01': '0.698'}),
 (3, {'A01': '0.705', 'A05': '0.114', 'B01': '0.698'}),
 (4, {'A01': '0.705', 'A05': '0.114', 'B01': '0.698'}),
 (5, {'A01': '0.705', 'A05': '0.114', 'B01': '0.698'}),
 (6, {'A01': '0.705', 'A05': '0.114', 'B01': '0.698'}),
 (7, {'A01': '0.704', 'A05': '0.114', 'B01': '0.697'}),
 (8, {'A01': '0.703', 'A05': '0.114', 'B01': '0.697'}),
 (9, {'A01': '0.703', 'A05': '0.114', 'B01': '0.696'}),
 (10, {'A01': '0.702', 'A05': '0.114', 'B01': '0.696'}),
 (11, {'A01': '0.702', 'A05': '0.114', 'B01': '0.696'}),
 .
 .
 .
 (59, {'A01': '0.684', 'A05': '0.114', 'B01': '0.679'}),
 (60, {'A01': '0.683', 'A05': '0.114', 'B01': '0.678'})]

最后,遍歷字典(按讀數排序)並輸出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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM