Hướng dẫn python fileinput vs open
Bài toán cần xử lýViết chương trình nhập vào từ file input {Tên}, {Tuổi hiện tại} và xuất ra file output theo mẫu sau: “Vao 20 nam nua, tuoi cua {Tên} se la {Tuổi cần tìm}”. Kiến thức cần có
Định dạng đầu vàoDữ liệu đầu vào lưu trữ trong file "Bai1.10.inp" Gồm hai dòng:
Định dạng đầu raDữ liệu đầu ra lưu trữ trong file "Bai1.10.out" Gồm một dòng duy nhất hiển thị như sau:
Ví dụ
Gợi ý
Code mẫu
Kết luậnTrong bài này, Kteam đã hướng dẫn bạn cách viết chương trình nhập vào từ file input {Tên}, {Tuổi hiện tại} và xuất ra file output theo mẫu sau: “Vao 20 nam nua, tuoi cua {Tên} se la {Tuổi cần tìm}”. Ở bài tiếp theo, chúng ta sẽ cũng nhau học cách viết chương trình NHẬP TỪ FILE INPUT {TÊN}, {TUỔI HIỆN TẠI} VÀ XUẤT RA FILE OUTPUT THEO MẪU (CÓ XỬ LÝ ĐỊNH DẠNG ĐẦU VÀO). Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của bạn để phát triển bài viết tốt hơn. Đừng quên " Luyện tập - Thử Thách - Không ngại khó!" Tải xuốngProjectNếu việc thực hành theo hướng dẫn không diễn ra suôn sẻ như mong muốn. Bạn cũng có thể tải xuống PROJECT THAM KHẢO ở link bên dưới! Thảo luậnNếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng. Source code: Lib/fileinput.py This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see The typical use is: import fileinput for line in fileinput.input(encoding="utf-8"): process(line) This iterates over the lines of all files listed in All files are opened in text mode by default, but you can override this by specifying the mode parameter in the call to Changed in version 3.3: If Empty files are opened and immediately closed; the only time their presence in the list of filenames is noticeable at all is when the last file opened is empty. Lines are returned with any newlines intact, which means that the last line in a file may not have one. You can control how files are opened by providing
an opening hook via the openhook parameter to The following function is the primary interface of this module: fileinput. input (files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)¶Create an instance of the The with fileinput.input(files=('spam.txt', 'eggs.txt'), encoding="utf-8") as f: for line in f: process(line) Changed in version 3.2: Can be used as a context manager. Changed in version 3.8: The keyword parameters mode and openhook are now keyword-only. Changed in version 3.10: The keyword-only parameter encoding and errors are added. The following functions use the global state created by fileinput. filename ()¶Return the name of the file currently being read. Before the first line has been read, returns fileinput. fileno ()¶Return the integer “file descriptor” for the current file. When no file is opened (before the first line and between files), returns fileinput. lineno ()¶Return the cumulative line number of the line that has just been read. Before the first line has been read, returns fileinput. filelineno ()¶Return the line number in the current file. Before the first line has been read, returns fileinput. isfirstline ()¶Return fileinput. isstdin ()¶Return fileinput. nextfile ()¶Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect. fileinput. close ()¶Close the sequence. The class which implements the sequence behavior provided by the module is available for subclassing as well: classfileinput. FileInput (files=None, inplace=False, backup='', *,
mode='r', openhook=None, encoding=None, errors=None)¶Class With mode you can specify which file mode will be passed to The openhook, when given, must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. You cannot use inplace and openhook together. You can specify encoding and errors that is passed to A with FileInput(files=('spam.txt', 'eggs.txt')) as input: process(input) Changed in version 3.2: Can be used as a context manager. Deprecated since version 3.4: The Deprecated since version 3.8:
Support for Changed in version 3.8: The keyword parameter mode and openhook are now keyword-only. Changed in version 3.10: The keyword-only parameter encoding and errors are added. Optional in-place filtering: if the keyword argument The two following opening hooks are provided by this module: fileinput. hook_compressed (filename, mode, *, encoding=None, errors=None)¶Transparently opens files compressed with gzip and bzip2 (recognized by the extensions The encoding and errors values are passed to Usage example: Changed in version 3.10: The keyword-only parameter encoding and errors are added. fileinput. hook_encoded (encoding, errors=None)¶Returns a hook which opens each file with Usage
example: Changed in version 3.6: Added the optional errors parameter. Deprecated since version 3.10: This function is deprecated since |