Creating an array out of another array between 2 elements - python

I am reading a file in python using readlines()
lines = f.readlines()
How can I add all the components in lines that appear between 2 specific characters for example:
lines = [rose, 1 , 2 , 4 , 5, 6], garden, plants ]
I want to create an array out of lines such that:
array = [1,2,3,4,5,6]
How can I do it?

#Read File
file = open("testFile.txt", "r")
f_str=file.read()
# Find positions of [] in String
begin_pos= f_str.find('[')+1
end_pos= f_str.find(']')
# Get Subset of String and Split it by ',' in a Str List
f_str=f_str[begin_pos:end_pos].split(',')
#Str List to Int List
plist=list(map(int, f_str))
#Test it
print(plist)
print(type(plist[1]))

Following should help:
# Open File
with open('../input_file.txt') as f:
lines = f.readlines()
# Find the required attribute
for line in lines:
if line[:4] == 'data':
data = line.split(':')[1].strip()
break
# Split the content to make a list of INTEGERS
python_list = map(lambda x : int(x.strip()),data[1:-1].split(','))
It provides a list of Integers as the data is numerical.Thanks.

Try this:
with open('Path/to/file', 'r') as f:
content = f.readlines()
data = content[8][7:].split(",")

Related

Reading from text file and storing in array [duplicate]

This question already has answers here:
Create new list from nested list and convert str into float
(4 answers)
Closed 3 years ago.
If I have a text file containing the following numbers:
5.078780 5.078993
7.633073 7.633180
2.919274 2.919369
3.410284 3.410314
How can read it and store it in an array, so that it becomes:
[[5.078780,5.078993],[7.633073,7.633180],[2.919274,2.919369],[3.410284,3.410314]]
with open('test.txt', 'r') as file:
output = [ line.strip().split(' ') for line in file.readlines()]
# Cast strings to floats
output = [[float(j) for j in i] for i in output]
print(output)
should give the desired output:
[[5.07878, 5.078993], [7.633073, 7.63318], [2.919274, 2.919369], [3.410284, 3.410314]]
Approach:
Have a result list = []
Split the text by newlines \n.
Now in a for-loop
split each line by a space char and assign to a tuple
append tuple to the result list
I'm refraining from writing code here to let you work it out.
This should do
with open ("data.txt", "r") as myfile:
data=myfile.readlines()
for i in range(len(data)):
data[i]=data[i].split()
You first want to retrieve the file content in an array of string (each string is one line of the file)
with open("myfile.txt", 'r') as f:
file_content = f.readlines()
Refer to open doc for more: https://docs.python.org/3/library/functions.html#open
Then you want to create a list
content_list = []
And then you want to fill it with each string, when each string should be split with a space(using split() function) which make a list with the two values and add it to content_list, use a for loop !
for line in file_content:
values = line.split(' ') # split the line at the space
content_list.append(values)
By the way, this can be simplified with a List Comprehension:
content_list = [s.split(' ') for s in file_content]
This should work,
with open('filepath') as f:
array = [line.split() for line in f.readlines()]
Python provides the perfect module for this, it's called csv:
import csv
def csv_to_array(file_name, **kwargs):
with open(file_name) as csvfile:
reader = csv.reader(csvfile, **kwargs)
return [list(map(float, row)) for row in reader]
print(csv_to_array('test.csv'))
If you later have a file with a different field separator, say ";", then you'll just have to change the call to:
print(csv_to_array('test.csv', delimiter=';'))
Note that if you don't care about importing numpy then this solution is even better.
To convert to this exact format :
with open('filepath', 'r') as f:
raw = f.read()
arr = [[float(j) for j in i.split(' ')] for i in raw.splitlines()]
print arr
outputs :
[[5.07878, 5.078993], [7.633073, 7.63318], [2.919274, 2.919369], [3.410284, 3.410314]]
with open('blah.txt', 'r') as file:
a=[[l.split(' ')[0], l.split(' ')[1] for l in file.readlines() ]

Nested lists in python containing a single string and not single letters

I need to load text from a file which contains several lines, each line contains letters separated by coma, into a 2-dimensional list. When I run this, I get a 2 dimensional list, but the nested lists contain single strings instead of separated values, and I can not iterate over them. how do I solve this?
def read_matrix_file(filename):
matrix = []
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split()
matrix.append(line)
return matrix
result:
[['a,p,p,l,e'], ['a,g,o,d,o'], ['n,n,e,r,t'], ['g,a,T,A,C'], ['m,i,c,s,r'], ['P,o,P,o,P']]
I need each letter in the nested lists to be a single string so I can use them.
thanks in advance
split() function splits on white space by default. You can fix this by passing the string you want to split on. In this case, that would be a comma. The code below should work.
def read_matrix_file(filename):
matrix = []
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split(',')
matrix.append(line)
return matrix
The input format you described conforms to CSV format. Python has a library just for reading CSV files. If you just want to get the job done, you can use this library to do the work for you. Here's an example:
Input(test.csv):
a,string,here
more,strings,here
Code:
>>> import csv
>>> lines = []
>>> with open('test.csv') as file:
... reader = csv.reader(file)
... for row in reader:
... lines.append(row)
...
>>>
Output:
>>> lines
[['a', 'string', 'here'], ['more', 'strings', 'here']]
Using the strip() function will get rid of the new line character as well:
def read_matrix_file(filename):
matrix = []
with open(filename, 'r') as matrix_letters:
for line in matrix_letters:
line = line.split(',')
line[-1] = line[-1].strip()
matrix.append(line)
return matrix

How to reverse a list without specific elements being reversed in python

f = open (FilePath, "r")
#print f
with open(FilePath, "r") as f:
lines = f.readlines()
#print lines
for iterms in lines:
new_file = iterms[::-1]
print new_file
it gives me a result like this:
7340.12,8796.4871825,0529.710635,751803.0,fit.69-81-63-40tuo
original list is like this:
out04-32-45-95.tif,0.330693,536043.5237,5281852.0362,20.2260
it is supposed to be like this:
20.2260, ...........out04-32-45-95.tif
You should be using your for loop like:
for iterms in lines:
new_file = ','.join(iterms.split(',')[::-1])
print new_file
Explanation:
In your current code, the line iterms[::-1] reverses the entire string present in your line. But you want to only reverse the words separated by ,.
Hence, you need to follow below steps:
Split the words based on , and get list of words:
word_list = iterms.split(',')
Reverse the words in the list:
reversed_word_list = word_list[::-1]
Join the reversed wordlist with ,
new_line = ','.join(reversed_word_list)

Python: read from file into list

I want my program to read from a .txt file, which has data in its lines arranged like this:
NUM NUM NAME NAME NAME. How could I read its lines into a list so that each line becomes an element of the list, and each element would have its first two values as ints and the other three as strings?
So the first line from the file: 1 23 Joe Main Sto should become lst[0] = [1, 23, "Joe", "Main", "Sto"].
I already have this, but it doesn't work perfectly and I'm sure there must be a better way:
read = open("info.txt", "r")
line = read.readlines()
text = []
for item in line:
fullline = item.split(" ")
text.append(fullline)
Use str.split() without an argument to have whitespace collapsed and removed for you automatically, then apply int() to the first two elements:
with open("info.txt", "r") as read:
lines = []
for item in read:
row = item.split()
row[:2] = map(int, row[:2])
lines.append(row)
Note what here we loop directly over the file object, no need to read all lines into memory first.
with open(file) as f:
text = [map(int, l.split()[:2]) + l.split()[2:] for l in f]

Read a file and create arrays with the words of each column

I've the following file, salida.txt which has different number of columns in this example, just 2.
cil HUF, M1 NSS,
442, 1123,
20140130, 2014012,
20140131, 2014014,
I want to read the file and add each column into a new array. I wan't to have this:
['cli HUF', '442', '20140130', '20140131']
[' M1 NSS', '1123', '2014012', '2014014']
What I've tried so far:
file = open('salida.txt', 'r')
for line in file:
// add them to the arrays
I'm having problems to handle the number of arrays (it's not always 2, depends on the number of columns of the file) and taking each word from the line to add in the proper array. If I to put inside de loop print line[0] it prints me the entire line, and I want to handle it word by word.
arrays = []
with open('salida.txt', 'r') as wordfile:
for line in wordfile:
# Split the line on commas.
words = line.split(',')
for count, word in enumerate(words):
# Remove any whitespace.
word = word.strip()
# That might leave a blank string, e.g. at the end.
if word:
# Do we need to add another array to our list of arrays?
if count == len(arrays):
arrays.append([])
arrays[count].append(word)
print arrays
Strip the last commas, and then split the line in the center commas:
list1, list2 = [], []
file = open('salida.txt', 'r')
for line in file:
w1, w2 = line.strip(',').split(', ')
list1.append(w1)
list2.append(w2)
import csv
with open('salida.txt') as f:
whatYouWant = zip(*list(csv.reader(f)))[:-1]
There you go:
file = open('salida.txt', 'r')
lines = file.readlines()
file.close()
arrays = []
words = lines[0].split(",")
for i in range(0,len(words)):
arrays.append([words[i]])
for i in range(1,len(lines)):
words = lines[i].split(",")
for j in range(0,len(words)):
arrays[j].append(words[j])

Categories