Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8570847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:39:05+00:00 2026-06-11T18:39:05+00:00

I have a text file just say text1 text2 text text text text text

  • 0

I have a text file just say

text1 text2 text text
text text text text

I am looking to firstly count the number of strings in the file (all deliminated by space) and then output the first two texts. (text 1 text 2)

Any ideas?

Thanks in advance for the help

Edit: This is what I have so far:

>>> f=open('test.txt')
>>> for line in f:
    print line
text1 text2 text text text text hello
>>> words=line.split()
>>> words
['\xef\xbb\xbftext1', 'text2', 'text', 'text', 'text', 'text', 'hello']
>>> len(words)
7
if len(words) > 2:
    print "there are more than 2 words"

The first problem I have is, my text file is: text1 text2 text text text

But when i pull the output of words I get:
[‘\xef\xbb\xbftext1’, ‘text2’, ‘text’, ‘text’, ‘text’, ‘text’, ‘hello’]

Where does the ‘\xef\xbb\xbf come from?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:39:06+00:00Added an answer on June 11, 2026 at 6:39 pm

    To read a file line by line, just loop over the open file object in a for loop:

    for line in open(filename):
        # do something with line
    

    To split a line by whitespace into a list of separate words, use str.split():

    words = line.split()
    

    To count the number of items in a python list, use len(yourlist):

    count = len(words)
    

    To select the first two items from a python list, use slicing:

    firsttwo = words[:2]
    

    I’ll leave constructing the complete program to you, but you won’t need much more than the above, plus an if statement to see if you already have your two words.

    The three extra bytes you see at the start of your file are the UTF-8 BOM (Byte Order Mark); it marks your file as UTF-8 encoded, but it is redundant and only really used on Windows.

    You can remove it with:

    import codecs
    if line.startswith(codecs.BOM_UTF8):
        line = line[3:]
    

    You may want to decode your strings to unicode using that encoding:

    line = line.decode('utf-8')
    

    You could also open the file using codecs.open():

    file = codecs.open(filename, encoding='utf-8')
    

    Note that codecs.open() will not strip the BOM for you; the easiest way to do that is to use .lstrip():

    import codecs
    BOM = codecs.BOM_UTF8.decode('utf8')
    with codecs.open(filename, encoding='utf-8') as f:
        for line in f:
            line = line.lstrip(BOM)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a text file with a list of strings like so:
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have this code in PHP writing views to a text-file and just increasing
I just started using/learning Python and have some questions. I have a text file
I' ve got a problem with Haskell. I have text file looking like this:
Let say i have a text file with today is today but tomorrow is
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Say I have the following style of lines in a text file: 12 34
we have a text file containing binary values. say, we have a file file.txt,it
I have many source/text file, say file.cpp or file.txt . Now, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.