File keluaran biasanya disimpan dari atas ke bawah, artinya yang terbaru ada di bawah. Mungkin ada situasi di mana kami ingin membaca file dari bawah ke atas. Pada artikel ini kita akan membahas beberapa cara membaca dan mencetak konten file secara terbalik
Baris yang diakhiri dengan titik
Ini adalah contoh file teks kami bernama random. txt yang akan kita gunakan
The scene is beautiful. Fuel
prices are high.
Food supply does
not look good. This food is
delicious.
_4
Tujuan kami adalah untuk membacanya seperti di bawah ini
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful.Baris yang tidak berakhir dalam satu titik
Selain itu kami juga akan memproses file teks yang berisi baris yang tidak diakhiri titik, karena kalimat tidak selalu diakhiri titik dalam satu baris
The scene is beautiful. Fuel
prices are high.
Food supply does
not look good. This food is
delicious.
5
Tujuan kami adalah membalikkannya tetapi dalam kalimat lengkap
1. Membalikkan garis yang berakhir dengan periode
Semua kode ditulis dengan Python 3. 10. 2
Kami akan menggunakan acak. txt dalam kode kami untuk menunjukkan bagaimana baris teks dibalik
The scene is beautiful. Fuel
prices are high.
Food supply does
not look good. This food is
delicious.
_4
Tujuan kami adalah membalikkannya seperti di bawah ini
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful.2 utilitas kami berfungsi untuk membaca file dan mencetak daftar
kode
from typing import List def read_file(fn: str) -> List: """ Read text file and save each line in a list. """ data: List = [] with open(fn) as f: for line in f: data.append(line.rstrip()) return data _kode
from typing import List def read_list(data: List): """ Print each item in the data list. """ for t in data: print(t)1. 1. Menggunakan terbalik
kode
print('reverse:\n') fn = 'random.txt' data = read_file(fn) data.reverse() # list is reversed inplace read_list(data)keluaran
1. 2. Menggunakan terbalik
kode
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. 0keluaran
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. 11. 3. Menggunakan diurutkan
kode
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. 2keluaran
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. _31. 4. Menggunakan irisan
kode
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. _4keluaran
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. 51. 5. Menggunakan readlines dan terbalik
Metode ini tidak disarankan untuk file besar karena membaca seluruh teks di memori
kode
keluaran
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. 71. 6. Menggunakan while dan pop
kode
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. _8keluaran
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful. _92. Membalikkan garis yang tidak berakhir pada periode
The scene is beautiful. Fuel prices are high. Food supply does not look good. This food is delicious. 5
The scene is beautiful. Fuel prices are high. Food supply does not look good. This food is delicious.Tujuan kami adalah untuk membalikkannya, tetapi dalam kalimat untuk dengan mudah memahami isinya
This food is delicious. Food supply does not look good. Fuel prices are high. The scene is beautiful.Ada banyak pendekatan untuk menyelesaikan ini, tetapi kami hanya akan menggunakan tokenizer kalimat nltk
kode
The scene is beautiful. Fuel prices are high. Food supply does not look good. This food is delicious. _2keluaran
The scene is beautiful. Fuel prices are high. Food supply does not look good. This food is delicious. _33. Kesimpulan
Ada banyak cara untuk membaca terbalik teks dalam sebuah file. Kita bisa menggunakan python terbalik, diurutkan, daftar mengiris dan lain-lain. Kami juga belajar cara membaca dan mencetak file teks secara terbalik di mana baris tidak berakhir dalam satu titik menggunakan sentense tokenizer dari perpustakaan nltk