1

I am new to Python, so please do not laugh at this question......

I have some arrays in a file, shown below

100 23 35 44 47 511
100 60 77 68 45 76
100 97 99 89 91 14
100 53 65 

I have read the file and got every row with the following code,

f = file('new.txt')
lines = f.readlines()
f.close()
results = []
for line in lines:
  print line

but in order to treat them as an input of a function, as below,

clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])

I think I need to format my arrays to make every array in the square brackets ([ ]) and add commas among them. The final format I need is like this

clf.fit ([[100,23,35,44,47,511], [100,60,77,68,45,76], [100,97,99,89,91,14]], [100,53,65])

How can achieve it?

2
  • results isn't used in the above example. Where does clf1 come from? Commented Nov 13, 2015 at 20:35
  • @Dan Sorry I did not write the whole code piece. clf = linear_model.LinearRegression(), which is a function of scikit-learn Commented Nov 13, 2015 at 20:54

2 Answers 2

1

Just split each line into a list and create a list of lists (an array, essentially).

final_array = []

with open('new.txt') as f:
    for line in f:
        temp_list = [int(x) for x in line.strip().split()]
        if len(temp_list) > 0:  # don't append an empty list (blank line)
            final_array.append(temp_list)

print final_array

You may want to do some additional sanity checking but this accomplishes the basic idea.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! The final array of this code is [['100', '23', '35', '44', '47', '\n'], ['100', '60', '77', '68', '77\n'], ['100', '97', '99', '89', '91', '\n'], ['100', '100', '43', '64', '64']]. How can I get rid of '\n'?
@KathyLee a list comprehension can do it in the same line of code, see update
@KathyLee see edit I just made, forgot to convert strings to integers and not have an int combined with newline char. Fixed in latest edit
Thank you. You save me again! I just tried the function but had "TypeError". But it works with your latest edit! :)
@KathyLee one last edit to make it a little more robust, you can add more checks as needed. If this solves your issue don't forget to mark as accepted. If not, let us know what is lacking.
|
1

You can use numpy.loadtxt() to load your file as an array.Or if you don't want to use numpy you better to use csv module to load your data and them convert them to integer.

import csv 

with open('new.txt') as f:
   spam_reader = csv.reader(f,delimiter=' ')
   my_array = [map(int,row) for row in spam_reader]

Note that if you are not sure about the validation of your data the preceding code will raise a ValueError, which in that case you need to handle the exception using a try-except expression.

my_array = []
for row in spam_reader:
   try:
       my_array.append(map(int,row))
   except ValueError:
       # do stuff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.