Dưới đây là nhiều tối ưu hóa và ứng dụng của phong cách Python thích hợp để làm cho mã của bạn gọn gàng hơn rất nhiều. Tôi đã đặt một số mã tùy chọn bằng mô -đun csv
, mong muốn hơn so với phân tích thủ công. Tôi cũng đã đặt một chút tốt đẹp ____10, nhưng tôi không sử dụng các thuộc tính mà sau đó cung cấp. Tên của các phần của tên gọi là không chính xác, bạn sẽ cần sửa chúng.
import csv
from collections import namedtuple
from time import localtime, strftime
# Method one, reading the file into lists manually [less desirable]
with open['grades.dat'] as files:
grades = [[e.strip[] for e in s.split[',']] for s in files]
# Method two, using csv and namedtuple
StudentRecord = namedtuple['StudentRecord', 'id, lastname, firstname, something, homework1, homework2, homework3, homework4, homework5, homework6, homework7, exam1, exam2, exam3']
grades = map[StudentRecord._make, csv.reader[open['grades.dat']]]
# Now you could have student.id, student.lastname, etc.
# Skipping the namedtuple, you could do grades = map[tuple, csv.reader[open['grades.dat']]]
request = open['requests.dat', 'w']
cont = 'y'
while cont.lower[] == 'y':
answer = raw_input['Please enter the Student I.D. of whom you are looking: ']
for student in grades:
if answer == student[0]:
print '%s, %s %s %s' % [student[1], student[2], student[0], student[3]]
time = strftime['%a, %b %d %Y %H:%M:%S', localtime[]]
print time
print 'Exams - %s, %s, %s' % student[11:14]
print 'Homework - %s, %s, %s, %s, %s, %s, %s' % student[4:11]
total = sum[int[x] for x in student[4:14]]
print 'Total points earned - %d' % total
grade = total / 5.5
if grade >= 90:
letter = 'an A'
elif grade >= 80:
letter = 'a B'
elif grade >= 70:
letter = 'a C'
elif grade >= 60:
letter = 'a D'
else:
letter = 'an F'
if letter = 'an A':
print 'Grade: %s, that is equal to %s.' % [grade, letter]
else:
print 'Grade: %.2f, that is equal to %s.' % [grade, letter]
request.write['%s %s, %s %s\n' % [student[0], student[1], student[2], time]]
print
cont = raw_input['Would you like to search again? ']
print 'Goodbye.'
Đã tạo: tháng 5-09, 2021 Các chuỗi trong Python có thể được định nghĩa là cụm các ký tự Unicode được đặt trong các trích dẫn đơn hoặc đôi. Giống như các ngôn ngữ lập trình phổ biến khác, Python cũng có một ký tự mới được biểu thị bằng
1 để xóa ký tự dòng mới khỏi chuỗi trong Pythonstr1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
2 để xóa ký tự dòng mới khỏi chuỗi trong Pythonstr1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
3 để xóa ký tự dòng mới khỏi chuỗi trong Pythonstr1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
4. Về cơ bản, nó được sử dụng để theo dõi đỉnh cao của một dòng và sự xuất hiện của một dòng mới trong một chuỗi.str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Các ký tự Newline cũng có thể được sử dụng trong F-String. Hơn nữa, theo tài liệu Python, các câu lệnh in thêm một ký tự dòng mới ở cuối chuỗi theo mặc định.
Hướng dẫn này sẽ thảo luận về các phương pháp khác nhau để loại bỏ một ký tự dòng mới khỏi một chuỗi trong Python.
Sử dụng chức năng str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
1 để xóa ký tự dòng mới khỏi chuỗi trong Python
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
1 được sử dụng để loại bỏ cả dòng mới và hàng đầu từ chuỗi mà nó đang được vận hành. Nó cũng loại bỏ các khoảng trắng ở cả hai bên của chuỗi.Mã sau sử dụng hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
1 để xóa ký tự dòng mới khỏi chuỗi trong Python.str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Output:
Starbucks has the best coffee
Hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
8 có thể được sử dụng thay vì hàm dải nếu chỉ cần xóa các ký tự dòng mới. Các ký tự mới hàng đầu không bị ảnh hưởng bởi chức năng này và vẫn như vậy.Mã sau sử dụng hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
8 để xóa ký tự dòng mới khỏi chuỗi trong Python.str1 = "\n Starbucks has the best coffee \n"
newstr = str1.rstrip[]
print[newstr]
Output:
Starbucks has the best coffee
Sử dụng chức năng str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
2 để xóa ký tự dòng mới khỏi chuỗi trong Python
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Còn được gọi là phương pháp vũ lực, nó sử dụng chức năng
Starbucks has the best coffee
1 và hàm str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
2. Chúng tôi tìm kiếm ký tự mới str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
4 dưới dạng chuỗi bên trong chuỗi và thay thế thủ công từ mỗi chuỗi bằng sự trợ giúp của vòng lặp Starbucks has the best coffee
1.Chúng tôi sử dụng một danh sách các chuỗi và thực hiện phương pháp này trên nó. Danh sách là một trong bốn kiểu dữ liệu tích hợp được cung cấp trong Python và có thể được sử dụng để lưu trữ nhiều mục trong một biến duy nhất.
Mã sau sử dụng hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
2 để xóa ký tự dòng mới khỏi chuỗi trong Python.list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]
rez = []
for x in list1:
rez.append[x.replace["\n", ""]]
print["New list : " + str[rez]]
Output:
New list : ['Starbucks', 'has the best', 'coffee ']
Sử dụng chức năng str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
3 để xóa ký tự dòng mới khỏi chuỗi trong Python
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Mô -đun
Starbucks has the best coffee
7 cần nhập vào mã Python để sử dụng hàm str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
3Mô-đun
Starbucks has the best coffee
7 là một mô-đun tích hợp trong Python, liên quan đến biểu thức chính quy. Nó giúp thực hiện nhiệm vụ tìm kiếm một mẫu trong một chuỗi cụ thể nhất định.Hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
3 về cơ bản được sử dụng để lấy một chuỗi con và thay thế sự xuất hiện của nó trong chuỗi bằng một chuỗi con khác.Mã sau sử dụng hàm
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
3 để xóa ký tự dòng mới khỏi chuỗi trong Python.#import the regex library
import re
list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]
rez = []
for sub in list1:
rez.append[sub.replace["\n", ""]]
print["New List : " + str[rez]]
Output:
________số 8