简体   繁体   中英

Reading a section of a line in a file in python 2.7

I am using python 2.7 because that is what my professor is having us use.

I am analyzing flag data and each line contains 30 attributes about each flag.

I am only concerned with the 1st and 11th-17th attributes, but am not sure how to read those in and store them without the other ones I am not concerned with.

I am also pretty new to python so this could be a simple task I am just unaware of so if any suggestions help, I really appreciate it.

def getColors():
    f = open('flag.data.txt')

An example of one line in the file:

Afghanistan,5,1,648,16,10,2,0,3,5,1,1,0,1,1,1,0,green,0,0,0,0,1,0,0,1,0,0,black,green

Why not try:

def getColors():
    arr=[]
    f = open('flag.data.txt','r')
    for line in f: 
        line_arr = line.split(',')
        arr.append([line_arr[0]] + [line_arr[i] for i in range(10, 17)])
    return arr

Based on your answers, I would suggest something like this so:

from __future__ import with_statement

attributes = []
with open('flag.data.txt','r') as f:
    for line in f: 
        data = line.strip().split(',')
        attributes.append([data[0]] + data[10:17])

In the end, attributes array will have the cleaned out data you expect.

If you can use numpy, np.loadtxt can be handy for problems like these:

import numpy as np 
from StringIO import StringIO

data = """Afghanistan,5,1,648,16,10,2,0,3,5,1,1,0,1,1,1,0,green,0,0,0,0,1,0,0,1,0,0,black,green"""

result =  np.loadtxt(StringIO(data),dtype=str,delimiter=',',usecols=(0,10,11,12,13,14,15,16))

returns:

array(['Afghanistan', '1', '1', '0', '1', '1', '1', '0'], dtype='|S11')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM