0

I have created an error with my list formatting. I have a value of numbers that the list is referring to from a separate text document. It pulls all of the values correctly, and forms it into a list, however the formatting is very 'off'. If I were to have the numbers 55.34, 7.55, and 48.9 from this text document printed into a list, I get an output of [['55.34'], ['7.55'], ['48.90']] I want to get rid of these quotes and these brackets around them. The numbers are considered a string, but it is to be converted back to a float later in the program, as I want to add all of these numbers together.

I've tried the list.remove command, but it doesn't seem to work. This is a simplified version of what I am trying to work with:

with open("history.txt", 'r') as f:
            deposList = [line.rstrip('\n') for line in f.readlines()]
            deposList = [line.split(',') for line in deposList]
            list = deposList
            print(list)

History.txt is the text file. The first iteration of the deposList gets ride of all of the \n formatting that separated the lines in the text document. I then created a list with these outputs and printed it for verification. I want the list to look like this: [55.34, 7.55, 48.90]

dtwomey
  • 11
  • 5

2 Answers2

0

I can't test it due to missing 'history.txt' file but it should work.

with open("history.txt", 'r') as f:
    deposList = [line.rstrip('\n') for line in f.readlines()]
    deposList = [line.split(',') for line in deposList]
    list_ = [item for sublist in deposList for item in sublist]
    print(list_)
Frank
  • 1,959
  • 12
  • 27
0

You need to separate your formatting from your data:

depos = [['55.34'], ['7.55'], ['48.90']]

values = ', '.join(value[0] for value in depos)

format = '[{values}]'

print(format.format(values=values))
Peter Wood
  • 23,859
  • 5
  • 60
  • 99