Python: Write float array to a file - python

What is the cleanest and easiest way to write float array to a file?
This is what i was trying to do. mylist is array.
match = re.search(r"DeltaE =\s+(\S+).* Intensity =\s+(\S+)", line)
if match is not None:
self.deltae = float(match.group(1))
self.intensity = float(match.group(2))
mylist = [self.deltae, self.intensity]
with open("Test.txt", 'w') as myfile:
for range(sublist) in mylist:
myfile.write(', '.join(str(item) for item in sublist)+'\n')
print(mylist)
My list looks like that :
13.5423 0.0116934333
17.9918 0.0476088508
22.4523 0.0082869379
26.5963 0.00291399
34.1077 0.0222519629
39.0881 0.0027373305

Assuming that mylist is a 2-element list, you can use a generator expression:
with open("Test.txt", 'a') as myfile:
myfile.write(', '.join(str(item) for item in mylist)+'\n')
or the outdated map to map float to str:
with open("Test.txt", 'a') as myfile:
myfile.write(', '.join(map(str, mylist))+'\n')
If mylist is defined inside a loop, then you need to run this code inside the same loop to process all rows.

Related

I am trying to read in a file and then return a list of int values using the .readlines()

For a school assignment
we want to convert every item in the list to be an int using the int(val) function. This will involve first reading in the file, loading all the lines, and then looping through all lines building a second list of ints (use lst). You should return the list of ints
but I don't know how to do it.
This is what I have so far.
def file_int_list(file):
with open(file, 'r+') as f:
lst = []
lines = f.readlines()
for rows in lines:
lst.append(rows)
return lst
lst = []
lines = f.readlines()
for rows in lines:
lst.extend(list(map(int, rows.split(' '))))
return lst

Splitting a string of values into multiple strings

I'm currently working on a program that takes a group of lists from a csv file, and groups them together. The program I came up with is:
List_one = []
with open("trees.csv") as f:
skiplines = f.readline()
for line in f:
res = line.split(" ")
List_one.append(res)
for i in List_one:
(i[0]) = (i[0]).rstrip("\n")
print (List_one)
What I get now are a group of lists, but the problem is that these lists are strings and I want them as floats. The lists look like this:
[['1,8.3,70,10.3'], ['2,8.6,65,10.3'], ['3,8.8,63,10.2'], ['4,10.5,72,16.4'], ['5,10.7,81,18.8'], ['6,10.8,83,19.7'], ['7,11.0,66,15.6'], ['8,11.0,75,18.2'], ['9,11.1,80,22.6'], ['10,11.2,75,19.9'], ['11,11.3,79,24.2'], ['12,11.4,76,21.0'], ['13,11.4,76,21.4'], ['14,11.7,69,21.3'], ['15,12.0,75,19.1'], ['16,12.9,74,22.2'], ['17,12.9,85,33.8'], ['18,13.3,86,27.4'], ['19,13.7,71,25.7'], ['20,13.8,64,24.9'], ['21,14.0,78,34.5'], ['22,14.2,80,31.7'], ['23,14.5,74,36.3'], ['24,16.0,72,38.3'], ['25,16.3,77,42.6'], ['26,17.3,81,55.4'], ['27,17.5,82,55.7'], ['28,17.9,80,58.3'], ['29,18.0,80,51.5'], ['30,18.0,80,51.0'], ['31,20.6,87,77.0']]
As you guys can see I also can't use float() on list one either, because the list is a whole string on its own. Is there a way I can split the lists by indexing so I get:
['1', '8.3', '70', '10.3'].....
Any help is welcome.
"line.split(',')" split the string with "," and returns list.
for string '1,8.3,70,10.3' it will return [1, 8.3, 70, 10.3]
You can split the strings by the commas if you want. You should probably do everything before you append them to List_one though.
res = [float(x) for x in line.split(" ")[0].split(",")]
List_one.append(res)
Does this work how you want it to? Sorry I'm not sure what format the input is in so I'm kind of guessing
You could say:
res = line.split(" ")
# map takes a function as the first arg and a list as the second
list_of_floats = list(map(lambda n: float(n), res.split(",")))
# then you can
List_one.append(list_of_floats)
Which will still give you a nested list because you are pushing a list during each iteration of for line in f:, but each list would at least be floats as you've specified.
If you wanted to just get one flat list of floats instead of doing the initial line.split(' ') you could use regex to split the line read from the csv:
import re # at the top of your file
res = re.split(r'[\s\,]', line)
list_of_floats = list(map(lambda n: float(n), res))
List_one.append(list_of_floats)
This might help:
l =[['1,8.3,70,10.3'], ['2,8.6,65,10.3'], ['3,8.8,63,10.2'], ['4,10.5,72,16.4']]
l2 =[]
for x in l:
a =x[0].split(",")
l2.append(a)
print(l2)
Enjoy!

Read txt file and put in list with python

I have a file with text with only 0's and 1's. No space between them. Example:
0101110
1110111
I would like to read a character at a time and place them as a single element in a list of integers.
My code:
intlist = []
with open('arq.txt', 'r') as handle:
for line in handle:
if not line.strip():
continue
values = map(int, line.split())
intlist.append(values)
print intlist
handle.close()
My result:
[[101110], [1110111]]
Like if I transform the 0101110 in intlist = [0,1,0,1,1,1,0,1,1,1,0,1,1,1]. (without '\n')
You just need two changes. The first is, when you're making values, you need to take each individual character of the line, instead of the entire line at once:
values = [int(x) for x in line.strip() if x]
Now, if you run this code on its own, you'll get [[1, 0, ...], [1, 1, ...]]. The issue is that if you call list.append with another list as an argument, you'll just add the list as an element. You're looking for the list.extend method instead:
intlist.extend(values)
Your final code would be:
intlist = []
with open('arq.txt', 'r') as handle:
for line in handle:
if not line.strip():
continue
values = [int(x) for x in line.strip() if x]
intlist.extend(values)
print intlist
If all you have is numbers and linefeeds, you can just read the file, remove the linefeeds, map the resulting string to integers, and turn that into a list:
with open('arq.txt') as handle:
intlist = list(map(int, handle.read().replace('\n', '')))
print intlist
Here's one way you can do it. You also don't need to use handle.close() the context manager handles closing the file for you.
intlist = []
with open('arq.txt', 'r') as handle:
for line in handle:
if not line.strip():
continue
intlist[:] += [int(char) for i in line.split() for char in i]
print(intlist)
Use strip() for delete \n and filter with bool for deleting empty strings:
with open('test.txt') as f:
lines = map(lambda x: x.strip(), filter(bool, f))
print [int(value) for number in lines for value in number]
One possibility:
intlist = []
with open('arq.txt', 'r') as handle:
for line in handle:
for ch in line.strip():
intlist.append (ch)
print intlist

Appending a string in front of every item in the file using Python

The following is what I am trying to achieve:
my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
my_new_list = [ string + x for x in my_list]
print my_new_list
The problem is all my words are in a text file. Example:
foo
fob
faz
funk
How to read the text file and write a loop to append the word 'bar' in front of each word and generate a new file?
Like that?
with open("myfile.txt") as f, open("output.txt", "w") as out:
for line in f:
out.write("bar" + line)
You can get a list of lines in a file using readlines() and you can write lines to a file using writelines():
with open('/path/to/infile') as infile:
lines = infile.readlines()
string = 'bar'
my_new_list = [string + x for x in lines]
with open('/path/to/outfile', 'w') as outfile:
outfile.writelines(my_new_list)

Building a List from a text file in python

I am a python newbie.
I want to read a text file which reads something like this
1345..
245..
..456
and store it in a list of lists of integers. I want to keep the numbers and replaces the periods by 0s.How do i do it?
EDIT:
Apologize for the ambiguous output spec
p.s I want the output to be a list of list
[ [1,3,4,5,0,0],
[2,4,5,0,0],
[0,0,4,5,6]]
with open('yourfile') as f:
lst = [ map(int,x.replace('.','0')) for x in f ]
Which is the same thing as the following nested list-comp:
lst = [ [int(val) for val in line.replace('.','0')] for line in f]
Here I used str.replace to change the '.' to '0' before converting to an integer.
with open(file) as f:
lis=[[int(y) for y in x.replace('.','0').strip()] for x in f]
Here's an answer in the form of classic for loops, which is easier for a newbie to understand:
a_list = []
l = []
with open('a') as f:
for line in f:
for c in line.rstrip('\n').replace('.', '0'):
l.append(int(c))
a_list.append(l)
#next line
l = []
print a_list

Categories