python - 从列表项的末尾删除 \n
问题描述
我有这个代码
def return_not_existing_signals(list_of_signals,read_or_write) :
not_used_produced_signals = []
#print(list_of_signals)
if read_or_write =="read":
file_to_create= "c:\\path\\read.txt"
else:
file_to_create = "c:\\path\\write.txt"
for i in list_of_signals :
i.replace('\n','')
if not search_for_signals(i) :
not_used_produced_signals.append(i)
write_to_txt(not_used_produced_signals,file_to_create)
def search_for_signals(name_of_signal):
for filename in Path('c:\path').glob('**/*.h') :
with open(filename) as f:
if name_of_signal in f.read():
return True
return False
问题是其中一些list_of_signals
在末尾有\n(例如:test1234rwq4\n)
I.replace(\'n',' ' )
不工作
解决方案
来自文档:
string.replace(s, old, new[, maxreplace])
返回字符串 s 的副本, 其中所有出现的子字符串 old 都替换为 new。如果给出了可选参数 maxreplace,则替换第一个 maxreplace 出现。
因此,您需要将其视为,
i = i.replace('\n','')
推荐阅读
- python - 在 url base64 编码中添加有效负载及其签名,然后对其进行解码
- asp.net-mvc - Linq 无法正常工作,未在 Ajax My Linq Query 中获取任何数据
- c++ - 对 unordered_map 使用 equal_range
- php - 我们如何使用 PHP 中的 GD 库将 html 表格转换为 png?
- c# - 将 HttpClientHandler.AutomaticDecompression 与 WebApplicationFactory.CreateClient() 一起使用
- makefile - 目标中的 Makefile ifndef 变量
- javascript - 如何使用 ExcelJS 读取 xlsx 文件?
- vim - vim 在带有 *.py 文件的目录中崩溃
- facebook - 共享到我管理的 Facebook 页面的内容仅对我可见
- javafx - JavaFX:setDisable(true) DatePicker 问题