1
我有一個文件,我想得到行的長度。Python len()不好
但是,當我得到的長度:
8 10 11 9 9 30 11 12 11 10 10 8 8 26 13等等
有我的代碼:
competition = []
with open('verseny.txt') as f:
competitor_number= int(next(f))
for line in f:
shots = line.split()
competition.append(str(shots))
for num,shots in enumerate(competition, 1):
if '++' in shots:
print(num, end=",")
print("\n")
for shots in competition:
print(len(shots))
的verseny.txt
內容是:
21
+--+
--+++-
-+--+--
++---
-++--
--+-+++----------------+-+
+-++--+
-+-+++-+
-+--+-+
--+++-
-+--+-
++--
-+--
-++----------------+-+
+-++-+--+
-+-+++-
-+--+-+-
++---+
-++-+-
--+-+++---+-------------+-+
+-++--+
這是解決方案! :D謝謝! – Giton22
或'line.rstrip()'只能移除到右側。 –
@ Giton22如果這是解決方案,只需接受答案。 –