-1

Given a text file named pop.txt. The content is below:

0       1650   
10      1750
20      1860
30      2070
40      2300
50      2560
60      3040
70      3710
80      4450
90      5280
100     6080
110     6870

I would like to read it in the get_data function. The output should be like [0,1650],[10,1750]...[110,6870].

def get_data(path,name):
    with open(path + file, 'r') as canary:
        x = canary.read().splitlines()
    return x

But the output is not what I want. Could you please help me fix my code?

General Grievance
  • 4,555
  • 31
  • 31
  • 45

4 Answers4

0
def get_data(path,name):
    with open(path + file, 'r') as canary:
        x = canary.readlines()
    parsed_data = []
    for line in x:
        parsed_data.append(line.split())
    return parsed_data 

If those spaces between the identifiers are actually tabs, you can replace line.split() with line.split('\t')

As an aside, you should consider using canary.readlines() instead of canary.read().splitlines() as outlined here.

EDIT: I'm not sure if you want your numbers to be strings or integers, but you can cast them to int by using [int(i) for i in line.split()]

PeptideWitch
  • 2,239
  • 14
  • 30
  • 1
    instead of `[int(i) for i in line.split()]`, use `map(int, line.split())` also `f'{path}{file}'` is the fastest way to concat strings. – OneMadGypsy Feb 15 '23 at 06:14
0

You should use map+split to split each line into two strings and then int to parse as integers.

filePath = 'table.txt'
def get_data(path):
    with open(path, 'r') as canary:
        lines = canary.read().splitlines()
        
    return list(map(lambda x: [int(i) for i in x.split()], lines))

print(get_data(filePath))

Since your file is very small read().splitlines() is okay, but you should use readlines() and loop over it.

Devesh
  • 603
  • 2
  • 11
0

I would go with this:

def get_data(path,name):
    with open(path + file, 'r') as canary:
        # you can use this if you want a list with all the entries
        all_lines = []
        for line in canary:
            # this will produce a list of 2 elements (index, number) for each line in your file
            single_line = [int(x) for x in line.split()]
            # appending single line to overall list
            all_lines.append(single_line)
Carlo
  • 1,321
  • 12
  • 37
0

You can use the pathlib module and a list comprehension to effectively turn the function into one statement:

from pathlib import Path
from pprint import pprint

def get_data(path, name):
    return [list(map(int, line.split())) for line in open(Path(path)/name)]

pprint(get_data('.', 'pop.txt'))

Output:

[[0, 1650],
 [10, 1750],
 [20, 1860],
 [30, 2070],
 [40, 2300],
 [50, 2560],
 [60, 3040],
 [70, 3710],
 [80, 4450],
 [90, 5280],
 [100, 6080],
 [110, 6870]]
martineau
  • 119,623
  • 25
  • 170
  • 301