Membalik urutan baris dalam file python

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

Membalik urutan baris dalam file python

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

The scene is beautiful.
Fuel prices are high.
Food supply does not look good.
This food is delicious.
_

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

The scene is beautiful. Fuel
prices are high.
Food supply does
not look good. This food is
delicious.

Tujuan kami adalah membalikkannya tetapi dalam kalimat lengkap

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.

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

The scene is beautiful.
Fuel prices are high.
Food supply does not look good.
This food is delicious.
_

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

reverse:

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.

1. 2. Menggunakan terbalik

kode

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
0

keluaran

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
1

1. 3. Menggunakan diurutkan

kode

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
2

keluaran

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
_3

1. 4. Menggunakan irisan

kode

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
_4

keluaran

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
5

1. 5. Menggunakan readlines dan terbalik

Metode ini tidak disarankan untuk file besar karena membaca seluruh teks di memori

kode

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
6

keluaran

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
7

1. 6. Menggunakan while dan pop

kode

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
_8

keluaran

This food is delicious.
Food supply does not look good.
Fuel prices are high.
The scene is beautiful.
_9

2. 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.
_2

keluaran

The scene is beautiful. Fuel
prices are high.
Food supply does
not look good. This food is
delicious.
_3

3. 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

Bagaimana Anda membalik urutan baris dalam file Python?

Pengguna harus memasukkan nama file
File dibuka menggunakan fungsi open() dan semua baris disimpan dalam daftar
fungsi reversed() menghasilkan iterator terbalik
Semua baris kemudian dicetak dalam urutan terbalik menggunakan for loop dan fungsi rstrip() menghapus semua spasi kosong dari akhir baris

Bagaimana kita bisa menampilkan konten file teks dalam urutan terbalik?

Pendekatan. .
Inisialisasi panjang teks sebelumnya sebagai 0
Temukan panjang garis saat ini dan tambahkan ke panjang sebelumnya. .
Ulangi langkah di atas hingga akhir file
Inisialisasi array panjang dari pesan yang diberikan dalam file yang diberikan

Bagaimana Anda mencetak dalam urutan terbalik dengan Python?

LANGKAH 1. Deklarasikan dan inisialisasi array
LANGKAH 2. Loop melalui array dalam urutan terbalik yaitu, loop akan dimulai dari (panjang array - 1) dan berakhir pada 0 dengan mengurangi nilai i sebanyak 1
LANGKAH 3. Cetak elemen arr[i] di setiap iterasi

Apa yang dilakukan reverse () dengan Python?

Metode reverse() membalik urutan pengurutan elemen .