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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:43:16+00:00 2026-05-30T19:43:16+00:00

Python beginner here. I’ve looked around and found similar questions but can’t quite cobble

  • 0

Python beginner here. I’ve looked around and found similar questions but can’t quite cobble enough solutions together for my particular problem. I’m trying to take the contents of a text file that contains user input building names:

GRG
FAC
MEZ

…that correspond to a line in another text file containing their coordinates:

"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382

…and either append or create a new text file that will look like this:

"Abbrev","Xcoord","Ycoord"
"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382

…which will get passed to my GIS software and create a point feature class.

This is what I’ve got so far, cobbled together from other questions I’ve found

inputfile = 'C:\\testing\\inputlist.txt'
datafile = file('C:\\testing\\bldglist.txt')

with open(inputfile,'r') as f:
    inputlist = [line.strip() for line in f]
    print inputlist    #I'm only confident up to this point.
    x = len(inputlist)

outputlist = []

for line in datafile:
    while x >= 0:    #basically I want to iterate through my list
        if inputfile[x] in line:
            outputlist.append(line.strip())
            x = x - 1
print outputlist 
#Once I get the above working I'll work on the output

So far the only thing I’ve managed to accomplish with this code is to make my computer flip out due to a MemoryError I think. Any help is greatly appreciated.

Edit:
Thanks to Dan, this is the solution I worked out:

datafile = 'C:\\testing\\bldglist.txt'
inputfile = 'C:\\testing\\inputlist.txt'

with open(inputfile, 'r') as f:
    names = set([line.strip() for line in f])
    print names

outputlist = []

with open(datafile, 'r') as f:
    for line in f:
        name = line.split(',')[0]
        if name[1:-1] in names:
            outputlist.append(line)
        else:
            print "Nothing found"
    print outputlist

Edit

I still have one outstanding problem. I need the outputlist to match the order of the inputlist. Unfortunately, the output order matches the order the buildings are found in the datafile. How can I make the outputlist in the same order as the inputlist?

  • 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-30T19:43:17+00:00Added an answer on May 30, 2026 at 7:43 pm

    First of all, store the names in a set, so you don’t have to iterate over a list for every line in the second file:

    with open(inputfile, 'r') as f:
        names = set([line.strip() for line in f])
    

    Then you can build your output list. You can of course also write it directly to the output file, if you don’t need to do any more processing on the data:

    outputlist = []
    
    with open(datafile, 'r') as f:
        for line in f:
            name = line.split(',')[0]
            # The string slicing is to remove the "" that surrounds the name.
            if name[1:-1] in names:
                outputlist.append(line)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came here to ask a simple, very beginner question regarding python. I just
I am a beginner and got an issue, really head around now. Here is
Beginner in python, but been programming for about 5 years now. I suspect I
I am beginner in python and facing this problem. So how i can break
I'm a python beginner, and I'm curious how can I send a dictionary through
I'm python beginner: how can I iterate over csv files in one directory and
Beginner programmer here. So bear with me. I have a simple python program. print
Python beginner here, so I apologize if this question has a simple answer. (I
beginner to python here. I have 2 nested lists that I want to merge:
Python beginner here. I am using the matplotlib library to make graphs from tab

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.