简体   繁体   中英

Read data seperated with tab from file in python

If I have the following file:

Pig
06-13-01    56.2
06-13-02    59.2
06-13-03    54.3
.
.
.
Cow
06-13-01   201.2
06-13-02   204.1
06-13-03   205.6
.
.
.

and want to create an instance of an object with the data for the animal with the associated date and weight (value seperated by tab). How do I do that in my main program?

I have begun with this:

  with open(filnamn, encoding="utf-8") as file:
dateAndWeight = []
lines = fil.readlines()
lines = [line.rstrip() for line in lines]
stepsBetweenName = 68
numberOfAnimals = int(len(lines)/stepsBetweenName)`

But this is just the beginning. Does anyone have any advice?

Your data alternates between animal name and tab separated data. This is a good fit for itertools.groupby which creates its own iterators based on a condition such as column count.

In this example, groupby starts a new sub-iteration whenever the row count changes between 1 and not-1. When its 1, you know you have a new animal. When not-1, you have rows of data. Here, I just built a dictionary that maps animal name to its date/weight info.

import itertools
import io
import csv

# test file

file = io.StringIO("""Pig
06-13-01\t56.2
06-13-02\t59.2
06-13-03\t54.3
Cow
06-13-01\t201.2
06-13-02\t204.1
06-13-03\t205.6""")

# will hold `animal:[[date, weight], ...]` associations
animal_map = {}

# data is TSV file
reader = csv.reader(file, delimiter="\t")

# Group by rows of length 1 which start a new set of animal date, weight pairs
for new_animal, rows in itertools.groupby(reader, lambda row: len(row) == 1):
    if new_animal:
        # get animal from first row
        animal = next(rows)[0]
    else:
        # add animal and data to map
        animal_map[animal] = list(rows)
        del animal

print(animal_map)

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