Pythonを使ってテキストファイルをreadした後、特定の単語を含む行を除いて処理したいのですが...
if elseかなと思ったのですがうまく行かなくて困ってます…
読み込むテキストファイルはこんな感じです
AAA(要) 1....
XXX(不要) 1....
AAA 2....
XXX 2....
.
.
.
BBB(要) 547.....
XXX(不要) 547....
file = open('test.txt','r') string = file.readline() string = file.readline() x = [0]*10000 y = [0]*10000 z = [0]*10000 array_number = [0]*10000 #配列番号 tag = [0]*10000 #物質名 atom = [0]*10000 #原子名 chain = [0]*10000 #鎖 #最初の読み込み------------------------------------ cnt = 0 while string: cnt = int(string[6:11],10) if string[36] == ' ': string = file.readline() continue x[cnt] = float(string[31:38]) y[cnt] = float(string[39:46]) z[cnt] = float(string[47:54]) array_number[cnt] = float(string[22:26]) tag[cnt] = string[17:20] atom[cnt] = string[13:16] chain[cnt] = string[21] string = file.readline() print('READ FINISH.');
例えば「不要」を含む行を除きたいのでしょうか。
うまくいかないとうそのコードを載せてください。
import math
import itertools
import collections
#all_coordinates-----------------------------------
file = open('test.txt','r')
string = file.readline()
string = file.readline()
x = [0]*10000
y = [0]*10000
z = [0]*10000
array_number = [0]*10000 #配列番号
tag = [0]*10000 #物質名
atom = [0]*10000 #原子名
chain = [0]*10000 #鎖
#最初の読み込み------------------------------------
cnt = 0
while string:
cnt = int(string[6:11],10)
if string[36] == ' ':
string = file.readline()
continue
x[cnt] = float(string[31:38])
y[cnt] = float(string[39:46])
z[cnt] = float(string[47:54])
array_number[cnt] = float(string[22:26])
tag[cnt] = string[17:20]
atom[cnt] = string[13:16]
chain[cnt] = string[21]
string = file.readline()
print('READ FINISH.'); #読み込み完成すると表示
です。
if elseは除いてある元のプログラムです
コメントだとインデントが崩れてしまうので、コードは元の質問に追記して下さいませ。
追記しました!
質問本文にコードブロック(```)で囲んだうえで追記ください。質問は編集できます。
ログインが出来ず、返信が遅くなり申し訳ございません。
該当箇所のコードを追記しました。
宜しくお願い致します。
回答2件
あなたの回答
tips
プレビュー