0

Пожалуйста, помогите, я с ума схожу, как убрать из подсчета символов \n? И пробелы убрать при выводе

Please help, I'm going crazy, how can I remove \ n from the character count? And remove spaces in the output file = open("/usercode/files/books.txt", "r") lines = file.readlines() for line in lines: print (line[0],(len(line))-1) file.close()

26th Oct 2020, 10:45 AM
Elena Varvarchuk
Elena Varvarchuk - avatar
6 ответов
+ 1
To remove space between printed values use sep="". Like this: print(line[0],len(line)-1,sep="") If you are not sure that the last element is always '\n', you can just remove the trailing '\n' : line=line.rstrip('\n') print(line[0],len(line), sep="")
26th Oct 2020, 1:50 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
Thank you!!!!! Is work!!!!
28th Oct 2020, 6:32 PM
Elena Varvarchuk
Elena Varvarchuk - avatar
0
When you use a comma in a print statement, it's replaced by a space. Use a '+' as in: for line in lines: print(line[0] + (len(line) - 1))
26th Oct 2020, 11:28 AM
Slick
Slick - avatar
0
This + not work
26th Oct 2020, 11:31 AM
Elena Varvarchuk
Elena Varvarchuk - avatar
0
right, because its a number. add the string of the length of the line minus 1
26th Oct 2020, 11:32 AM
Slick
Slick - avatar
- 2
Тут я хэр знает, я по Html
26th Oct 2020, 10:46 AM
Ponchimo
Ponchimo - avatar