Home > Back-end >  Read files from txt
Read files from txt

Time:04-07

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?

CodePudding user response:

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.

CodePudding user response:

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()]

CodePudding user response:

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)
  • Related