[英]My nested list always adds an empty string
For some reason, it keeps adding an empty ' ' in the nested list.出于某种原因,它不断在嵌套列表中添加一个空的 ' '。
This is how the results are printed out:这是打印结果的方式:
[['Sara', '37', '32', '47', ''], ['Johan', '44', '29', '34', ''], ['Kalle', '33', '34', '34', ''], ['Oskar', '23', '47', '45', '']]
I have tried what is in this article but it doesn't work.我已经尝试过这篇文章中的内容,但它不起作用。 Not sure where the fault is.不知道错在哪里。 https://www.geeksforgeeks.org/python-remove-empty-strings-from-list-of-strings/ https://www.geeksforgeeks.org/python-remove-empty-strings-from-list-of-strings/
def results_from_file(file_name):
file_exist()
my_file = open(file_name, "r")
data = my_file.read().split("\n")
results = []
for row in data:
results.append(row.split(";"))
return results
As Aplet123 said:正如 Aplet123 所说:
Your lines probably all end in
;
您的台词可能都以;
Try this to fix:试试这个来修复:
replace代替
data = my_file.read().split("\n")
to至
data = my_file.read().replace(';\n','\n').split("\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.