Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a text file
#8
(Oct-12-2017, 08:38 PM)snippsat Wrote:
gruntfutuk Wrote:How about: (using Python 3)
Now do itertools also work for Python 2,and i guess he use Python 3 bye his use of print function.
itertools.islice is cool @gruntfutuk,but for this i had gone for a more simplistic approach as shown in other post with enumerate.
with open('in.txt') as f:
    for count,line in enumerate(f, 1):
        if count % 2:
            print(len(line.strip()))

Your approach is best @snippsat, I was just trying to provide a simpler iter alternative to what @DeaD_EyE suggested as an approach. As the source file is so short, it is all pretty academic. Were the file large, we would be looking for the most efficient approach, avoiding reading anything we don't need to.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Reading a text file - by fivestar - Oct-11-2017, 11:49 PM
RE: Reading a text file - by Larz60+ - Oct-12-2017, 12:33 AM
RE: Reading a text file - by wavic - Oct-12-2017, 11:26 AM
RE: Reading a text file - by DeaD_EyE - Oct-12-2017, 12:13 PM
RE: Reading a text file - by gruntfutuk - Oct-12-2017, 04:39 PM
RE: Reading a text file - by DeaD_EyE - Oct-12-2017, 06:55 PM
RE: Reading a text file - by snippsat - Oct-12-2017, 08:38 PM
RE: Reading a text file - by gruntfutuk - Oct-13-2017, 07:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,006 Mar-31-2021, 11:53 AM
Last Post: snippsat
  Reading a text until matched string and print it as a single line cananb 1 1,997 Nov-29-2020, 01:38 PM
Last Post: DPaul
  reading from a file looseCannon101 14 4,754 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Convert text from an image to a text file Evil_Patrick 5 4,217 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  Weird problem with reading from file and performing calculations pineapple999 1 2,956 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,718 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,830 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,247 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Reading of structured .mat (matlab) file sumit 2 3,369 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,103 May-07-2018, 04:12 PM
Last Post: toxicxarrow

Forum Jump:

User Panel Messages

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