python去除字符串最后的换行符‘\n’
s = s.replace('\n','').replace('\r','')
例子
取出gt.txt中的abcd等按顺序生成字典{‘0’:‘abcd’,‘1’:‘efg’},一般最后一个数据会有换行符需要去掉
gt.txt:
abcd xxx
efg xxx …
f = open('I:\\data_3\\gt.txt', 'r', encoding = 'UTF-8') labelstr = f.readlines() a = range(0,200) index = [str(i) for i in a] txt = [label.split(' ')[0].replace('\n','').replace('\r','') for label in labelstr] labeldic = dict(zip(index, txt))
附录:在 Python 中从字符串中删除换行符可以使用哪些方式?
Python 中的字符串可以定义为用单引号或双引号括起来的 Unicode 字符簇。
与其他流行的编程语言一样,Python 也有一个由 \n
表示的换行符。它主要用于跟踪一行的顶点和字符串中新行的出现。
换行符也可以在 f 字符串中使用。此外,根据 Python 文档,print 语句默认在字符串末尾添加换行符。
接下来我们介绍几种在 Python 中从字符串中删除换行符的不同方法。
一、在 Python 中使用 strip() 函数从字符串中删除换行符
strip()
函数用于从正在操作的字符串中删除尾随和前导换行符。它还删除字符串两侧的空格。
以下代码使用 strip()
函数从 Python 中的字符串中删除换行符。
str1 = "\n Starbucks has the best coffee \n" newstr = str1.strip() print(newstr)
输出:
Starbucks has the best coffee
如果只需要删除尾随的换行符,可以使用 rstrip()
函数代替 strip 函数。前导换行符不受此函数影响并保持原样。
以下代码使用 rstrip()
函数从 Python 中的字符串中删除换行符。
str1 = "\n Starbucks has the best coffee \n" newstr = str1.rstrip() print(newstr)
输出:
Starbucks has the best coffee
二、在 Python 中使用 replace() 函数从字符串中删除换行符
也称为蛮力方法,它使用for
循环和replace()
函数。我们在字符串中寻找换行符\n
作为字符串,并在for
循环的帮助下从每个字符串中手动替换它。
我们使用字符串列表并在其上实现此方法。列表是 Python 中提供的四种内置数据类型之一,可用于在单个变量中存储多个项目。
以下代码使用 replace()
函数从 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))
输出:
New list : ['Starbucks', 'has the best', 'coffee ']
三、在 Python 中使用 re.sub() 函数从字符串中删除换行符
re
模块需要导入到 python 代码中才能使用 re.sub()
函数
re
模块是 Python 中的内置模块,用于处理正则表达式。它有助于执行在给定的特定字符串中搜索模式的任务。
re.sub()
函数本质上用于获取子字符串并将其在字符串中的出现替换为另一个子字符串。
以下代码使用 re.sub()
函数从 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))
输出:
New List : ['Starbucks', 'has the best', 'coffee ']
以上是python如何去除字串最後的換行符號'\n'的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具