#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Python Read file. Make a List per line


    Hey

    So im wanting to read from a file and put each line of it into a list.

    Say I have a file named MyFile.txt or MyFile.csv containing the following three lines of numbers/decimals:
    Code:
    49.55,2,77.09,18,1,2.34,32.11
    33,11.22,33.21,56,76.55
    8,9,44.7,90.99,12.21,1.01
    I want the program to open the file then read line one and put it into a list ~ say L1list. I then want it to make L2list & L3list for lines 2 & 3. Once that's done I want to be able to work out the total for a list, average, max & min etc. I'm pretty confident with this part using sum (), len(),max(), min() etc.

    Its the populating of the lists that has me stuck. Im getting the wrong totals, lengths etc. Thought maybe its the full-stops or comers. but i need the decimal places and thought lists need the comers to separate.

    So far i have tried:
    Code:
    filename = 'MyFile.txt' 
    fin=open(filename,'r')
    L1list = fin.readline()
    L2list = fin.readline()
    L3list = fin.readline()
    Also:
    Code:
    L1list.append(fin.readline())
    Along with:
    Code:
    L1list = [i.strip().split() for i in fin.readlines()]
    A little stuck as to whats going wrong.

    Thanks in advance. Oh and im using Python 2.7.3

    Kim
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Something like this?
    Code:
    >>> with open('MyFile.txt') as f:
    ...     L = [list(map(float, line.strip().split(','))) for line in f]
    ... 
    >>> L
    [[49.55, 2.0, 77.09, 18.0, 1.0, 2.34, 32.11], [33.0, 11.22, 33.21, 56.0, 76.55], [8.0, 9.0, 44.7, 90.99, 12.21, 1.01]]
    >>> (L1list, L2list, L3list) = L
    >>> L1list
    [49.55, 2.0, 77.09, 18.0, 1.0, 2.34, 32.11]
    >>> L2list
    [33.0, 11.22, 33.21, 56.0, 76.55]
    >>> L3list
    [8.0, 9.0, 44.7, 90.99, 12.21, 1.01]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    you could store them in an array
    an display a specified line with ARRAY[i]

    Code:
    FILE = open("file.txt","rt")
    ARRAY = []
    for line in FILE:
        ARRAY.append(line)
    Yegers
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by Lux Perpetua
    Something like this?
    Code:
    >>> with open('MyFile.txt') as f:
    ...     L = [list(map(float, line.strip().split(','))) for line in f]
    Why you map() at all when you can use list comprehensions all the way?

    Code:
    L = [[float(num) for num in line.strip().split(',')] for line in f]
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)

IMN logo majestic logo threadwatch logo seochat tools logo