Home > Software engineering >  Loading a file into a list in Python
Loading a file into a list in Python

Time:11-04

I have the following problem: I have 7 lists denoting the days of the week and for each of them I want to load values from a .txt file.

Contents of the file:

[50, 54, 29, 33, 22, 100, 45, 54, 89, 75]

[80, 98, 40, 43, 43, 80, 50, 60, 79, 30]

[10, 7, 80, 43, 48, 82, 33, 55, 83, 80]

[15, 20, 38, 10, 36, 50, 20, 26, 45, 20]

[20, 25, 47, 18, 56, 70, 30, 36, 65, 28]

[122, 140, 245, 128, 156, 163, 90, 140, 150, 128]

[100, 130, 234, 114, 138, 156, 107, 132, 134, 148]

I have included code snippets below:

mon_sales = []
tue_sales = []
wed_sales = []
thu_sales = []
fri_sales = []
sat_sales = []
sun_sales = []

week_sales = [mon_sales, tue_sales, wed_sales, thu_sales,
              fri_sales, sat_sales, sun_sales]


def load_sales(file_path):
    """
    Loads the data in the sales list from a file.
    file_path specifies the path of the file to load
    Reports file exception if loading fails
    """

    print('Loading sales data from a file: ', file_path)

    print(week_sales)

    for day_sales in week_sales:
        day_sales.clear()

    print(week_sales)
    for day_sales in week_sales:
        try:
            with open('sales.txt', 'r') as input_file:
                for line in input_file:
                    stripped_line = line.strip() 
                    line_list = stripped_line.split('\n')
                    day_sales.append(line_list[0])
        except:
            print('omething went wrong while saving the data.')

    print(week_sales)

The problem is that instead of a line from the .txt file for the day of the week list, the program copies the contents of the file as an additional list on each day of the week.

I'm running out of ideas on how to change this, I'm just learning python and need your help to figure it out.

How to write a program to add values from only one line of the text file to each list with days of the week?

CodePudding user response:

Using json library:

sales.txt

[50, 54, 29, 33, 22, 100, 45, 54, 89, 75]
[80, 98, 40, 43, 43, 80, 50, 60, 79, 30]
[10, 7, 80, 43, 48, 82, 33, 55, 83, 80]
[15, 20, 38, 10, 36, 50, 20, 26, 45, 20]
[20, 25, 47, 18, 56, 70, 30, 36, 65, 28]
[122, 140, 245, 128, 156, 163, 90, 140, 150, 128]
[100, 130, 234, 114, 138, 156, 107, 132, 134, 148]

code

import json

with open('sales.txt') as input_file:
    result = [json.loads(s) for s in input_file]

[[50, 54, 29, 33, 22, 100, 45, 54, 89, 75], [80, 98, 40, 43, 43, 80, 50, 60, 79, 30], [10, 7, 80, 43, 48, 82, 33, 55, 83, 80], [15, 20, 38, 10, 36, 50, 20, 26, 45, 20], [20, 25, 47, 18, 56, 70, 30, 36, 65, 28], [122, 140, 245, 128, 156, 163, 90, 140, 150, 128], [100, 130, 234, 114, 138, 156, 107, 132, 134, 148]]

If your file really does have blank lines between each line of data use:

import json

with open('filename.csv') as input_file:
    def try_load(s):
        try:
            return json.loads(s)
        except json.decoder.JSONDecodeError:
            return None

    result = [s for s in map(try_load, input_file) if s]
  • Related