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

  • Home
  • SEARCH
  • 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 929051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:06:26+00:00 2026-05-15T20:06:26+00:00

I am new to programming and having some problem figuring out nested loops. I

  • 0

I am new to programming and having some problem figuring out nested loops. I have a list of data that I want to extract from a larger file. I am able to extract one item of data from the larger file successfully but I need to extract 100 different trials from this larger file of thousands of trials. Each trial is one line of data of the larger file. This is the program I have used to extract one line of data successfully one at a time. In this example it extracts the data for trial 1. It is based off of examples I have seen in prior questions and tutorials. The problem is that I don’t need trials 1-100, or any ordered pattern. I need trials 134, 274, 388, etc. It skips around. So I don’t know how to do a nested loop using the for statement if it doesn’t have a range that I can enter. Any help is appreciated. Thanks.

completedataset = open('completedataset.txt', 'r')

smallerdataset = open('smallerdataset.txt', 'w')


for line in completedataset:
    if 'trial1' in line: smallerdataset(line)


completedataset.close()
smallerdataset.close()

I’d really like to do it like this:

trials = (‘trial12’, ‘trial23’, ‘trial34’)

for line in completedataset:
for trial in trials:
if trial in line: smallerdataset(line)

but this isn’t working. Can anyone help me modify this program so that it works correctly?

  • 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-15T20:06:27+00:00Added an answer on May 15, 2026 at 8:06 pm

    You are going to run into some problems with the way you are specifying your trials. If you look for lines containing ‘trial1’, you will also get lines containing ‘trial123’. If you larger dataset is structured in some way you can trying looking for the trial number in a particular field. For instance, if the data is comma delimited you can make use of the csv package. Finally, using a generator expression instead of the loop will make things a little cleaner. Assuming that the trial number was in the first column of your dataset you could do something like:

    import csv
    
    trials = ['trial134', 'trial1', 'trial56']
    data = csv.reader(open('completedataset.txt'))
    
    with open('smalldataset.txt','w') as outf:
        csv.writer(outf).writerows(l for l in data if l[0] in trials)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Network Programming, and I am having a problem with some
I'm relatively new to Android programming, and am having a problem with inflation. I'm
I'm fairly new to C programming and having some difficulties wrapping my brain around
I'm a bit new to programming for iOS, and I'm having some trouble linking
I'm trying to learn some Erlang and having trouble figuring out the best approach
I am fairly new to object oriented programming, so I am still having some
I'm new to programming in Objective-C and have some C++ under my belt. As
I am new to programming and am having difficulty with understanding object lifetime /
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...
I'm new to programming, and I'm having trouble linking to a Boost library in

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.