0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

txtファイル読み込みの工夫メモ

Posted at

自分がtextファイルを読み込む時にいろいろと工夫した時のメモ。

案外、ファイルから読み込むところが1番大変だったりする。

まずは、読み込んだ時に改行で','を入れてリスト化する方法。

read.py

import sys

text_list = [ ]
with open("blog.txt","r",encoding='utf-8') as f:
    a = f.read().replace('\n' , ' , ')
    text_list.append(a)

print(text_list)

次、読み込むと改行ごとに配列にして多重配列を作っていく方法。

read_2.py

import sys 

text_list = []
for line in open("blog.txt","r",encoding='utf-8'):
    line = line.strip()
    line = line.split('\n')
    text_list.append(line)
print(text_list)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?