Membalikkan 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

Membalikkan 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 baris dalam file dengan Python?

Ketentuan .
Baca dari file teks dan ambil baris file seperti "A" dari file
Membalik urutan baris
Gabungkan string yang tidak terkait dengan setiap karakter baris file. (mantan. "R" --> "Contoh/R")
Buat file teks baru
Keluarkan string baru ke file teks baru
Ulangi untuk baris file baru jika perlu

Bagaimana cara membaca file dalam urutan terbalik?

Membaca Kata Mundur . Untuk ini, kita pertama-tama membaca baris-barisnya ke belakang dan kemudian menandai kata-kata di dalamnya dengan menerapkan fungsi balik . Dalam contoh di bawah ini kami memiliki token kata yang dicetak terbalik dari file yang sama menggunakan paket dan modul nltk.

Apakah Reverse () berfungsi pada string Python?

Fungsi bawaan () Terbalik . Namun, maksud utama dan kasus penggunaan reversed() adalah untuk mendukung iterasi balik pada iterables Python . Dengan string sebagai argumen, reversed() mengembalikan iterator yang menghasilkan karakter dari input string dalam urutan terbalik.

Apa yang dilakukan reverse () dengan Python?

Metode reverse() membalik urutan pengurutan elemen .