次のコードを使用して、ファイルを開き、必要な行を削除してからデータをファイルに書き出し、さらに下流の解析用にファイルを読み込みたいと思います。今(_io.TextIOWrapper)データの読み取り/印刷方法は?
with open("chr2_head25.gtf", 'r') as f,\
open('test_output.txt', 'w+') as f2:
for lines in f:
if not lines.startswith('#'):
f2.write(lines)
f2.close()
、(f2
)私はf2のデータを読み取り、パンダや他のモジュールでさらに処理をしたいが、データを読みながら、私は問題に実行しています。 (前回with
ブロックが終了したときに)あなたは、ファイルへのより多くの何もできないので、
data = f2 # doesn't work
print(data) #gives
<_io.TextIOWrapper name='test_output.txt' mode='w+' encoding='UTF-8'>
data = io.StringIO(f2) # doesn't work
# Error message
Traceback (most recent call last):
File "/home/everestial007/PycharmProjects/stitcher/pHASE-Stitcher-Markov/markov_final_test/phase_to_vcf.py", line 64, in <module>
data = io.StringIO(f2)
TypeError: initial_value must be str or None, not _io.TextIOWrapper
具体的にすることができますか? 'open(...)as f2.read()'の間にコードの2行目で 'f2.read()'を実行しようとしましたが、機能しませんでした。 – everestial007