[英]How to read unicode from text file and write respective string into exel file by using Python
[英]how to avoid linebreakers from unicode string read from text file in python
如何使用python從文本文件讀取的unicode文本中刪除換行符,即'\\ n'? 另外,如何測試列表的值是否在Unicode字符串中是換行符?
# Checks if the text is more than 0 symbols. And if the last symbol is "\n"
if len(test) > 0 and test[-1:]=="\n":
# If so, remove the last linebreak
test = test[0:-1]
要從文本文件中刪除換行符,可以使用rstrip():
with open('somefile.txt', 'r') as f:
for line in f:
print(line.rstrip())
要測試值是否為換行符:
for item in some_values:
if item == '\n':
do something
或者,您可以測試'\\ n'是否在一行中:
with open('somefile.txt', 'r') as f:
for line in f:
if '\n' in line:
print(line.rstrip())
Unicode字符串與標准字符串具有相同的方法,您可以使用line.replace(r'\\ n','')刪除'\\ n',並檢查unc中是否帶有'\\ n'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.