Đối với Python 3, cách để làm điều này không thêm dấu gạch chéo ngược và chỉ đơn giản là bảo tồn hello\nbobby\nsally\n 2, hello\nbobby\nsally\n 3, v.v. là:
a = 'hello\nbobby\nsally\n' a.encode('unicode-escape').decode().replace('\\\\', '\\') print(a)Mang lại một giá trị có thể được viết là CSV:
hello\nbobby\nsally\nTuy nhiên, dường như không có một giải pháp cho các nhân vật đặc biệt khác, tuy nhiên, có thể nhận được một \ trước họ. Đó là một người lập dị. Giải quyết điều đó sẽ phức tạp.
Ví dụ: để tuần tự hóa hello\nbobby\nsally\n 4 chứa danh sách các chuỗi có ký tự đặc biệt trong TextFile ở định dạng mà Bert mong đợi với CR giữa mỗi câu và một đường trống giữa mỗi tài liệu:
with open('sentences.csv', 'w') as f: current_idx = 0 for idx, doc in sentences.items(): # Insert a newline to separate documents if idx != current_idx: f.write('\n') # Write each sentence exactly as it appared to one line each for sentence in doc: f.write(sentence.encode('unicode-escape').decode().replace('\\\\', '\\') + '\n')Điều này xuất ra (cho tài liệu GitHub CodeSearchNet cho tất cả các ngôn ngữ được mã hóa thành câu):
Makes sure the fast-path emits in order. @param value the value to emit or queue up\n@param delayError if true, errors are delayed until the source has terminated\n@param disposable the resource to dispose if the drain terminates Mirrors the one ObservableSource in an Iterable of several ObservableSources that first either emits an item or sends\na termination notification. Scheduler:\n{@code amb} does not operate by default on a particular {@link Scheduler}. @param the common element type\n@param sources\nan Iterable of ObservableSource sources competing to react first. A subscription to each source will\noccur in the same order as in the Iterable. @return an Observable that emits the same sequence as whichever of the source ObservableSources first\nemitted an item or sent a termination notification\n@see ReactiveX operators documentation: Amb ...\ (\ newcommand {l} [1] {\ | #1 \ |} \ newcommand {vl} [1] {\ l {\ vec Re} \, (#1)} \ newCommand {i} [1] {\ operatorName {im} \, (#1)} \)
Một chuỗi theo nghĩa đen là nơi bạn chỉ định nội dung của một chuỗi trong một chương trình.
Ở đây, một chuỗi là một chuỗi theo nghĩa đen. Biến A là một biến chuỗi, hoặc, tốt hơn là Python, một biến trỏ đến một chuỗi.
Chuỗi chữ có thể sử dụng các dấu phân cách trích dẫn đơn hoặc kép.
>>> a = 'A string' # string literal with single quotes >>> b = "A string" # string literal with double quotes >>> b == a # there is no difference between these strings True
Chuỗi theo nghĩa đen với các dấu phân cách trích dẫn đơn có thể sử dụng báo giá kép bên trong chúng mà không cần thêm công việc.
>>> print('Single quoted string with " is no problem') Single quoted string with " is no problem
Nếu bạn cần một ký tự trích dẫn thực tế bên trong một chuỗi theo nghĩa đen được phân định bởi các trích dẫn đơn, bạn có thể sử dụng ký tự dấu gạch chéo ngược trước khi trích dẫn, để nói với Python không chấm dứt chuỗi:
>>> print('Single quoted string containing \' is OK with backslash') Single quoted string containing ' is OK with backslash
Tương tự như vậy cho trích dẫn kép:
>>> print("Double quoted string with ' is no problem") Double quoted string with ' is no problem >>> print("Double quoted string containing \" is OK with backslash") Double quoted string containing " is OK with backslash
Một số ký tự đi trước bởi một dấu gạch chéo ngược có ý nghĩa đặc biệt. Ví dụ:
>>> print('Backslash before "n", as in \n, inserts a new line character') #doctest: +NORMALIZE_WHITESPACE Backslash before "n", as in , inserts a new line character
Nếu bạn không muốn dấu gạch chéo ngược có ý nghĩa đặc biệt này, hãy đặt tiền tố theo nghĩa đen của bạn với ‘R, có nghĩa là Raw Raw:
>>> print(r'Prefixed by "r" the \n no longer inserts a new line') Prefixed by "r" the \n no longer inserts a new line
Bạn có thể sử dụng trích dẫn ba để kèm theo các chuỗi có nhiều hơn một dòng:
hello\nbobby\nsally\n 0
Báo giá ba có thể sử dụng dấu ngoặc kép đơn hoặc kép:
hello\nbobby\nsally\n 1