質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

14614閲覧

Pythonで変数を出力

mah3

総合スコア17

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/01/22 06:58

以下のコード

python

1 list = [] 2 a = pair_list[i][0] 3 b = pair_list[i][1] 4 c = pair_list[i][2] 5 d = pair_list[i][3] 6 i = i + 1 7 list = [list_intron[a],list_intron[b],list_intron[c],list_intron[d]] 8 print(list)

でlistをテキストファイルに出力したくて

python

1 list = [] 2 a = pair_list[i][0] 3 b = pair_list[i][1] 4 c = pair_list[i][2] 5 d = pair_list[i][3] 6 i = i + 1 7 list = [list_intron[a],list_intron[b],list_intron[c],list_intron[d]] 8 print(list) 9 file = open('t.txt', 'w') 10 file.write(list_intron[a],list_intron[b],list_intron[c],list_intron[d]) 11 file.close()

というコードを書いてみたのですが

TypeError: write() takes exactly one argument (4 given)

というエラーが出ました...
どうすれば出力できるでしょうか

ちなみに該当箇所までのコードが以下になります。

python

1file = open('2ikg.txt','r') 2string = file.readline() 3string = file.readline() 4 5x = [0]*10000 6y = [0]*10000 7z = [0]*10000 8array_number = [0]*10000 9tag = [0]*10000 10atom = [0]*10000 11chain = [0]*10000 12 13#最初の読み込み------------------------------------ 14cnt = 0 15while string: 16 cnt = int(string[6:11],10) 17 if string[36] == ' ': 18 string = file.readline() 19 continue 20 21 x[cnt] = float(string[31:38]) 22 y[cnt] = float(string[39:46]) 23 z[cnt] = float(string[47:54]) 24 array_number[cnt] = float(string[22:26]) 25 tag[cnt] = string[17:20] 26 atom[cnt] = string[13:16] 27 chain[cnt] = string[21] 28 29 string = file.readline() 30 31print('READ FINISH.'); 32 33#input_intron---------------------------------------- 34 35print('intronの数は?') 36intron_kazu = int(input()) #intron個数 37 38x_intron = []*(intron_kazu) 39y_intron = []*(intron_kazu) 40z_intron = []*(intron_kazu) 41list_intron = [] #intronの通しナンバー 42 43print('intron位置を全て入力して') 44print('例:入力➡ENTER➡入力➡ENTERの繰り返し') 45for i in range(intron_kazu): 46 47 intron_number = int(input()) #intronの残基番号を入力 48 list_intron.append(intron_number) 49 50 index = array_number.index(intron_number) #intorn分子の最初の通しナンバー 51 count = array_number.count(intron_number) #intron分子を構成する原子数 52 intron_finish = index + count #(intron原子の最後の通しナンバー + 1) 53 54 for j in range(index,intron_finish): 55 56 if atom[j] == 'CA ' and chain[j] == 'A' and array_number[j] == intron_number: #不斉炭素をintronの座標とする #鎖の設定はここ 57 58 #list_intron.append(j) #intron座標の通しナンバー 59 60 x_intron.append(x[j]) #intronのx座標 61 y_intron.append(y[j]) 62 z_intron.append(z[j]) 63 64#input_ligand---------------------------------------- 65 66print('リガンドは?') 67ligand_name = input() 68 69x_ligand = []*100 70y_ligand = []*100 71z_ligand = []*100 72 73index = tag.index(ligand_name) #一番初めのリガンド残基 74count = tag.count(ligand_name) #リガンドの数 75ligand_finish = index + count 76 77for i in range(index,ligand_finish): 78 x_ligand.append(x[i]) 79 y_ligand.append(y[i]) 80 z_ligand.append(z[i]) 81 82#combination_not_repeat------------------------------ 83 84l = range(0,intron_kazu,1) 85 86pair_list = []*100 87 88for pair in itertools.combinations(l, 4): 89 pair_list.append(pair) 90 91#print('intronの組み合わせは') 92#print(pair_list) 93 94#平面計算--------------------------------------------- 95 96#4つの組み合わせをabcdに入れる 97 98 99i = 0 100ligand_list = [] 101for repeat in range(len(pair_list)): 102 103 list = [] 104 #以下略 105

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

エラーが発生したときは、落ち着いてエラーを読んで、問題を理解しましょう。

write() takes exactly one argument (4 given)

write() はちょうど1個の引数を取ります。(4個与えられています)

投稿2019/01/22 07:40

mather

総合スコア6753

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mah3

2019/01/22 08:04

ご回答ありがとうございます。
guest

0

ベストアンサー

writeの引数は文字列かバイト列など(open時のモードに依存)を1つ取るだけです。

https://docs.python.jp/3/library/io.html

python

1# print関数のfile引数を使う方法 2print(*list, file=file) 3 4# 文字列に変換してwriteで書く方法 5file.write(str(list))

プログラムは雰囲気で書いても動きません。仕様を確認しながら、仕様を読めるレベルに達していなければ基礎から勉強しながら書いていってください。


また、listという変数名は組み込みのlistクラスを上書きしてしまうので使うのを控えてください。

投稿2019/01/22 07:05

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mah3

2019/01/22 08:04

ご回答ありがとうございます。 なるほど... わかりました! ありがとうございます。
guest

0

Python

1file.write(str(list_intron[a])+","+str(list_intron[b])+","+str(list_intron[c])+","+str(list_intron[d]))

投稿2019/01/22 07:04

yamato_user

総合スコア2321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mah3

2019/01/22 08:03

ご回答ありがとうございます。
mah3

2019/01/22 08:54

出力してみたのですが、このコードを使う場合改行コードを使うにはどうすればいいですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問