-5

input: data.txt looks like this

122.2 124.2 124.3 125.6 126.3 126.5 126.5 127.2 127.3
127.5 127.9 128.6 128.8 129.0 129.2 129.4 129.6 130.2
130.4 130.8 131.3 131.4 131.4 131.5 131.6 131.6 131.8
131.8 132.3 132.4 132.4 132.5 132.5 132.5 132.5 132.6
132.7 132.9 133.0 133.1 133.1 133.1 133.1 133.2 133.2
133.2 133.3 133.3 133.5 133.5 133.5 133.8 133.9 134.0
134.0 134.0 134.0 134.1 134.2 134.3 134.4 134.4 134.6
134.7 134.7 134.7 134.8 134.8 134.8 134.9 134.9 135.2
135.2 135.2 135.3 135.3 135.4 135.5 135.5 135.6 135.6
135.7 135.8 135.8 135.8 135.8 135.8 135.9 135.9 135.9
135.9 136.0 136.0 136.1 136.2 136.2 136.3 136.4 136.4
136.6 136.8 136.9 136.9 137.0 137.1 137.2 137.6 137.6
137.8 137.8 137.8 137.9 137.9 138.2 138.2 138.3 138.3
138.4 138.4 138.4 138.5 138.5 138.6 138.7 138.7 139.0
139.1 139.5 139.6 139.8 139.8 140.0 140.0 140.7 140.7
140.9 140.9 141.2 141.4 141.5 141.6 142.9 143.4 143.5
143.6 143.8 143.8 143.9 144.1 144.5 144.5 147.7 147.7

necessary output:

I would like to import all of this into a single list using python and IDE Spyder.

List1 = [122.2, 124.2, 124.3, ..., 147.7] 
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Okay, cool. Do you have a question? What part of this do you already know how to do? Can you open a file? Can you split strings? – ChrisGPT was on strike Mar 20 '23 at 22:06
  • There are many examples on the Internet explaining how you can parse files with Python. This is a trivial task, so you better check some tutorials out there. – user134 Mar 20 '23 at 22:06
  • Look into [this link](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) for how to open and read from files. Then, you can look into string parsing and splitting techniques to make it a list. Don't forget to convert the strings into floats so you can parse them. Have a good day! – rassar Mar 20 '23 at 22:08
  • Note that Spyder has nothing to do with this. Separate your editor from your code in your mind. – ChrisGPT was on strike Mar 20 '23 at 22:08

2 Answers2

0

Working on the assumption that your inputs will consistently look as your data.txt file does. i.e. single space (" ") separated values, then you could do the below, making use of the standard csv library.

import csv

# Create a list to store your loaded terms
terms = []

# Load the file
with open("data.txt", "r") as fh:
    # Assumes your file has a " " (delimiter) 
    reader = csv.reader(fh, delimiter = " ")

    # Loop through each row in the file

    for row in reader:
        terms += row
proudfeet
  • 1
  • 2
-1

First, use open to access your file. It is best to wrap it in a with statement to ensure the file is closed after.

Then, the file can be iterated over to access each line.

Finally, call str.split on each line to separate it into parts delimited by whitespace. This can be done in a nested list comprehension to obtain a single list as the result.

Putting it all together:

with open('data.txt') as f:
    list1 = [part for line in f for part in line.split()]
print(list1)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80