如何枚举文件的特定列

时间:2016-10-25 20:49:33

标签: python python-2.7

我试图枚举输入文件的第二列,但我的代码给出了以下错误。如何修复我的代码以获取以下输出文件?

错误:

  

回溯(最近一次调用最后一次):文件“./rename_atoms.py”,第7行,   在       print(item + index)TypeError:无法连接'str'和'int'对象

我的代码:

#!/usr/bin/python

with open ('input.gro', 'r') as f:
    for line in f:
        column=line.split()
        for index, item in enumerate(column[1]):
            print(item+index)

输入文件:

GRoups of Organic Molecules in ACtion for Science
   18
    1LIG      O    1   1.665   1.740   8.646
    1LIG      O    2   0.877   2.044   7.947
    1LIG      S    3   1.469   1.778   8.501
    1LIG      S    4   1.340   1.695   8.487
    1LIG      S    5   1.231   1.770   8.412
    1LIG      N    6   1.282   1.801   8.268
    1LIG      C    7   1.553   1.679   8.585
    1LIG      C    8   1.523   1.805   8.360
    1LIG      C    9   1.313   1.647   8.630
    1LIG      H   10   1.418   1.875   8.271
    1LIG      H   11   1.454   1.624   8.688
    1LIG      H   12   1.100   1.691   8.403
    1LIG      H   13   1.453   1.912   8.577
    1LIG      H   14   1.174   1.869   8.184
    1LIG      H   15   0.992   1.777   8.339
    1LIG      H   16   1.037   1.853   8.217
    1LIG      H   17   1.206   1.941   8.068
    1LIG      H   18   0.939   1.914   8.137
      0.00000   0.00000   0.00000

所需的输出文件:

GRoups of Organic Molecules in ACtion for Science
   18
    1LIG      O1    1   1.665   1.740   8.646
    1LIG      O2    2   0.877   2.044   7.947
    1LIG      S1    3   1.469   1.778   8.501
    1LIG      S2    4   1.340   1.695   8.487
    1LIG      S3    5   1.231   1.770   8.412
    1LIG      N1    6   1.282   1.801   8.268
    1LIG      C1    7   1.553   1.679   8.585
    1LIG      C2    8   1.523   1.805   8.360
    1LIG      C3    9   1.313   1.647   8.630
    1LIG      H1   10   1.418   1.875   8.271
    1LIG      H2   11   1.454   1.624   8.688
    1LIG      H3   12   1.100   1.691   8.403
    1LIG      H4   13   1.453   1.912   8.577
    1LIG      H5   14   1.174   1.869   8.184
    1LIG      H6   15   0.992   1.777   8.339
    1LIG      H7   16   1.037   1.853   8.217
    1LIG      H8   17   1.206   1.941   8.068
    1LIG      H9   18   0.939   1.914   8.137
      0.00000   0.00000   0.00000

2 个答案:

答案 0 :(得分:1)

问题出在这一行:

  for index, item in enumerate(column[1]):
>>      print(item+index)

您正在尝试添加int(index)和字符串(item)。如果您只想将它​​们打印在一起,只需执行以下操作:

print(item, index)

请注意,这将默认在参数之间添加空格。要删除它,请使用sep参数,并将其设置为空字符串。

然而,这可能不是你想要的,因为输出会是这样的:

O 0
O 0
S 0

......等等。

所以,这样做:

for i, l in enumerate(f):
    column = l.split()
    print(column[1],i,sep='')

这将是:

O0
O1
S2

......等等。

几乎就在那里。为了获得所需的输出,我们需要保持每个元素的计数。所以,尝试这样的事情:

from collections import defaultdict

counts = defaultdict(int)
for l in f:
    column = l.split()
    counts[column[1]] += 1
    print(column[1], counts[column[1]], sep="")

这应该打印:

O1
O2
S1

......等等。

答案 1 :(得分:0)

您无法连接int和字符串。它们是+函数的不兼容类型。您需要将索引从int转换为string,然后将它们连接起来。在第7行,尝试:

// Scene and Camera
var scene = new THREE.Scene(); 
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000);

// Renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

// Firt geometry with material and added to the scene.
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh(geometry, material);
scene.add( cube );

// Seconde geometry with material and added to the scene as well.
var mysphere = new THREE.SphereGeometry(5, 32, 32);
var color = new THREE.Color('#ee7800');
var hex = color.getHex;
var sphereMaterial = new THREE.MeshLambertMaterial( { color: hex } );
var sphere = new THREE.Mesh(mysphere, sphereMaterial);
sphere.position.y = 10;
scene.add( sphere );

// Better camera position
camera.position.z = 5;


// Render all and move the cube
function render() {
  requestAnimationFrame( render );

  cube.rotation.x += 0.03;
  cube.rotation.y += 0.01;

  renderer.render( scene, camera);
};

render();