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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:30:52+00:00 2026-05-18T09:30:52+00:00

It’s safe to say I’m a noob in programming and python, so I really

  • 0

It’s safe to say I’m a noob in programming and python, so I really need help handling a file.

My file is a dat file with an integer, a string and a float in each line, and I need to compare the first int with other ints and the float with some other value, I need to have the values as numbers and not as part of a string to perform some mathematical operations.

This is the code I’ve done, but I’ve looked all over the googles and I can’t find a function that does this:

readfile = open('file_being_read.dat').read()

def parsa_lista(file_to_read):
    converted = []
    for line in file_to_read:
       #conversion should happen here and write it to the list named "converted" 
       #my google-fu has failed me..
    return converted

print parsa_lista(readfile)

The file looks like this, but spans some 600 lines. Also, I’m going about this in a learn as I go basis, and I was really incapable of finding help, it might have something to do with the lack of some basic knowledge in data types or something.

This is the output of the list, as printed with “%r”:

...
249 LEU 89.81637573242188\n
250 ALA 6.454087734222412\n
251 ILE 42.696006774902344\n
252 VAL 39.9482421875\n
253 LEU 58.06844711303711\n
254 SER 6.285697937011719\n
255 HIS 22.92508316040039\n
256 THR 49.1857795715332\n
257 ASN 15.033650398254395\n
258 SER 12.086835861206055\n
259 VAL 28.70435905456543\n
260 VAL 39.53983688354492\n
261 ASN 18.63718605041504\n
262 PRO 15.275177955627441\n
263 PHE 120.84526062011719\n
264 ILE 26.20943260192871\n
265 TYR 16.6826114654541\n
266 ALA 34.382598876953125\n
267 TYR 179.9381103515625\n
268 ARG 77.62599182128906\n
269 ILE 45.021034240722656\n
270 ARG 133.72328186035156\n
...

Hope you guys can help me, even some general guidelines on how I should go about this in splitting strings and comparing values will be much appreciated.

  • 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-18T09:30:52+00:00Added an answer on May 18, 2026 at 9:30 am

    Ignacio’s answer is basically completely correct, and he posted it before I even started typing. However, let me explain his two-liner in a little more detail.

    Reading a file

    First, a critique of your code:

    readfile = open('file_being_read.dat').read()
    

    This will read out your entire file into a giant string. When you try to iterate over this string, you will iterate over it letter by letter. Change that line to this instead:

    readfile = open('file_being_read.dat')
    

    Now, when you iterate over this file object, you’ll be reading the file line-by-line.

    Tokenising

    You’ve found that iterating over a file gets you the text line-by-line. Now you need to split each line into those three values.

    If the values are separated by whitespace (like your data file excerpt), Python makes this very easy with the str.split method.

    >>> line
    '249 LEU 89.81637573242188\n'
    >>> line.split()
    ['249', 'LEU', '89.81637573242188']
    

    Any amount or type (tab, space) of whitespace between these values is fine. In fact, even the trailing newline gets stripped off. So now you have a list of three strings.

    Interpreting

    Next you need to convert the strings to integers and floating point numbers. Here, use the built-in functions int and float.

    >>> vals[0]
    '249'
    >>> int(vals[0])
    249
    >>> vals[2]
    '89.81637573242188'
    >>> float(vals[2])
    89.816375732421875
    

    At this point, you just need to package up these values into a tuple and add them to converted.

    datum = int(vals[0]), vals[1], float(vals[2])
    >>> datum
    (249, 'LEU', 89.816375732421875)
    

    Why a tuple instead of a list? Lists are mutable: you can add and remove elements. This probably isn’t what you need.

    (You probably usually see parentheses around a tuple literal. This is one of the few times when the order of operations make them unnecessary. You can put braces around the entire right side of the assignment and it will work just fine.)

    Putting it together

    def parsa_lista(file_to_read):
        converted = []
        for line in file_to_read:
            vals = line.split()
            datum = int(vals[0]), vals[1], float(vals[2])
            converted.append(datum)
        return converted
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.