Python Forum
Strip does not work when applied on a string read from a text file.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strip does not work when applied on a string read from a text file.
#2
What is desired result?

  • Why create so many variables if you need only one?
  • Why iterate over all lines in file and retain only last row status (every iteration of for-loop overwrites value of check and you will have it only for last row)?.
  • What do you mean by "9 character string becomes 19 character due to added spaces when code reads it from the file"?

First word in line is 9 characters (except in line 9 where it's 10)

with open('data_log.txt', 'r', encoding='UTF-8') as f:
    for line in f:
        value = line.split()[0]
        print(len(value))
9
9
9
9
9
9
9
9
10
9
9
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Strip does not work when applied on a string read from a text file. - by perfringo - Dec-27-2018, 07:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,915 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,494 Nov-09-2023, 10:56 AM
Last Post: mg24
  Need to replace a string with a file (HTML file) tester_V 1 788 Aug-30-2023, 03:42 AM
Last Post: Larz60+
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,179 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  read file txt on my pc to telegram bot api Tupa 0 1,148 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,135 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,335 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,805 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,214 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,662 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020