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 6069563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:50:09+00:00 2026-05-23T09:50:09+00:00

New to python and trying to learn the ropes of file i/o. I am

  • 0

New to python and trying to learn the ropes of file i/o.

I am working with pulling lines from a large (2 million line) file in this format:

56fr4
4543d
4343d
5irh3

Here is the function I’m using to return a code:

def getCode(i):
    with open("test.txt") as f:
        for index, line in enumerate(f):
            if index == i:
                code = # what does it equal?
                break
    return code

Once the index gets to the right spot (i), what syntax do I use to set the code variable?

  • 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-05-23T09:50:09+00:00Added an answer on May 23, 2026 at 9:50 am

    code = line.strip()

    will assign code to the line number that is equal to i while removing the trailing new line.

    you also need to update your code a bit

     def getCode(i):
        with open('temp.txt', 'r') as f:
                 for index, line in enumerate(f):
                         if index == i:
                                 code = line.strip()
                                 return code
    

    why you need .strip()

    >>> def getCode(i):
    ...     with open('temp.txt') as f:
    ...             for index, line in enumerate(f):
    ...                     if index == i:
    ...                             code = line
    ...                             return code
     ... 
    >>> getCode(2)
    "                  'LINGUISTIC AFFILIATION',\n"
    

    yes, ” ‘LINGUISTIC AFFILIATION’,” is in my current temp.txt’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to python and trying to learn the ropes of file i/o. Working with
I am pretty new to Python world and trying to learn it. This is
I am pretty new to python and working with firmata I am trying to
I'm brand new to Python and trying to learn it by replicating the following
I am new to python and trying to learn. I am trying to implement
I am new to Python and trying to learn the language structure. I understand
I'm new to python and trying to read /sys/devices/system/cpu/cpuX/cpufreq/vdd_levels from a Linux system and
I'm pretty new to python and am trying to grab the ropes and decided
I am trying to learn Python lists. In this code I am trying to
Im new two python and am trying to grow a dictionary of dictionaries. I

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.