Converting file data to nested list - python

I have a string of numbers that I would like to convert to a nested list. So far, I have
with open('lifedata.txt') as f:
table_data = [ line.split() for line in f]
print(table_data)
If the text document consists of numbers ordered like this,
0000000
0010000
0001000
0111000
0000000
0000000
The code I have so far only creates a nested list that looks like, [['0000000'], ['0010000'], ['0001000'], ['0111000'], ['0000000'], ['0000000']]
But instead, I wanted it took be [[0,0,0,0,0,0,0],[],[]] and so on. I also do not know how to convert the string into an integer. I'm just very confused on how I should manipulate the original text document to do what I want it to.

This is what is happening:
>>> "0000000".split()
['0000000']
Instead, call int() on every character in each string:
[[int(c) for c in line.strip()] for line in f]
Or, via map():
[list(map(int, line.strip())) for line in f]

Use this ,it will return a list of containing data from each line of txt.
open('lifedata.txt').readlines()

Related

How to read Lines of text from a file into a 2D matrix in python?

I want to read a text file that contains the following:
--------------------
---+---+---+--+-----
-------------+------
++-----------+------
-+-+----+------+----
--------------------
-----------+-------+
------+----+-------+
+-------------------
--+--------+------+-
I want to not only split this data into separate lines, but I want to split it into separate characters as well. For example, I want the data to read into the matrix as follows:
[
['-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'],
['-','-','-','+','-','-','-','+','-','-','-','+''-','-','+','-','-','-','-','-',],
...
]
This would end up being a 10 by 20 matrix
I am willing and able to use any libraries at my disposal.
I have tried looping through the file after reading it, and making a list of characters, and storing the list of characters into a parent list, but this just makes a list of a list, but I want to make a list of many lists (in this case, a list of 10 rows with 20 columns (or characters) in each list)
you can try this:
with open("file.txt","r") as f:
print([[y for y in x[:-1]] for x in f.readlines()])
It will create a list of many lists, the x[:-1] is to escape the "\n" at the end of each line.
Use list inside a list comprehension to convert each line to a list.
with open("file.txt") as f:
grid = [list(line.strip()) for line in f]

Getting specific element(s) from after read from file

After I read from file:
with open(fileName) as f:
for line in f:
print(line.split(",")) #split the file into multiple lists
How do I get some specific element(s) from those lists?
For example, only elements with index[0 to 3], but discard/ignore any elements after that.
If you want to save the first three items in each line, you could use a list comprehension
with open(fileName) as f:
firstitems = [line.rstrip().split(",")[0:3] for line in f]
Note that the rstrip() is needed to remove the final newline character, if there are fewer than four items in a line. Note that the "items" are all strings, even if they look like other types. If you want integers, for example, you will need to convert them to integers.
Then you can print them:
for line in firstitems:
print(line)
Try the below code:
with open('f.txt') as f:
print('\n'.join([i for i in f.read().split(',')[0:3]]))

Load text file python could not convert string to float

I have a text file that looks like this:
(1064.2966,1898.787,1064.2986,1898.787,1064.2986,1898.785,1064.2966,1898.785)
(1061.0567,1920.3816,1065.1361,1920.2276,1065.5847,1915.9657,1065.4726,1915.2927,1061.0985,1914.3955,1058.1824,1913.9468,1055.6028,1913.9468,1051.0044,1916.19,1051.5651,1918.8817,1056.0514,1918.9939,1058.9675,1919.6668,1060.8741,1920.4519)
etc (all rows have different lengths)
when I use
np.loadtxt(filename,dtype=float,delimiter=',')
I get
ValueError: could not convert string to float: (1031.4647
I think np.loadtxt expects numbers so it does not know how to convert a value which starts with a '(', I think you have two choices here:
lines = []
with open('datafile') as infile:
for line in infile:
line = line.rstrip('\n')[1:-1] # this removes first and last parentheses from the line
lines.append([float(v) for v in line.split(',')])
in this way you end up with lines which is a list of lists of values (i.e. lines[0] is a list of the values on line 1).
The other way to go is modifying the data file to remove the parentheses, which you can do in many ways depending on the platform you are working on.
In most Linux systems for instance you can just do something along the lines of this answer
EDIT: as suggested by #AlexanderHuszagh in the comments section, different systems can have different ways of representing newlines, so a more robust solution would be:
lines = []
with open('datafile') as infile:
file_lines = infile.read().splitlines()
for line in file_lines:
lines.append([float(v) for v in line[1:-1].split(',')])
You got the error because of the parentheses, you can replace it this way:
s = open(filename).read().replace('(','').replace(')','')
This return a list of arrays:
arrays = [np.array(map(float, line.split(","))) for line in s.split("\n")]

Trying to read a txt file with numbers into a list and then sort using Python

I have a one line txt file, file1.txt, that has a series of 10 numbers as such;
10,45,69,85,21,32,11,71,20,30
I want to take these numbers from the txt file and then add them to a list and then sort the numbers in ascending order.
I have tried
myfile1 = open('file1.txt', 'r').readlines()
but this seems to give me a list of length 1, which obviously can't be sorted.
In [101]: myfile1
Out[101]: ['10,45,69,85,21,32,11,71,20,30']
I'm guessing there is something wrong with how I am reading the text file however I can't seem to find a suitable way.
.readlines() does what it says: it reads the file in line by line. In your example, there is only one line, so the length is 1.
With that one line, you need to split on commas:
with open(file1.txt,'r') as myfile:
for line in myfile:
print sorted(map(int, line.split(',')))
Or, if you have multiple lines with lots of numbers:
data = []
with open(file1.txt,'r') as myfile:
for line in myfile:
data.extend(map(int, line.split(',')))
print sorted(data)
Here I use with with keyword to open the file, which can be iterated over line by line. Then, use the the split method of strings on each line, which returns a list of strings. Then, I use map to convert these strings into integers by applying the int type casting function to each item in the list. This list can then be sorted. Make sure to take a look at the string methods page on the Python documentation.
A test without the input file:
numbers = "10,45,69,85,21,7,32,11,71,20,30"
data = []
data.extend(map(int, numbers.split(',')))
print sorted(data)
prints
[7, 10, 11, 20, 21, 30, 32, 45, 69, 71, 85]
A little obfuscated to do as a 1-liner, but basically:
with open('file1.txt', 'r') as f:
data = sorted(map(int, f.readline().split(',')))
What this does:
Read 1 line: f.readline()
Split that line on ',' characters: .split(',')
Map the list of string to int values: map(int, list)
Sort the list of int: sorted(list)

converting a list of strings into a tuple

Hello i am trying to do a dictionary by using Python,
What needs to be done is python reads a text file which has values inside such as:
good buono
What I have done was, open the file with file function and replace tabs and add reversed comma to create a list so it looks like
["('good', 'buono')", "('afternoon', 'pomeriggo')",... and so on
but the problem is type of each word translation is not a tuple, it is string when I am trying to see 1st element(number 0) it shows me the value as
"('good', 'buono')"
which is a string. I need to use dict formula so that i can convert the type into dictionary but I cannot because it is list of strings(has to be list of tuples)
So how can I convert that list of strings into list of tuples?
Assuming that every pair of words is on a separate line in your file, you could do this:
mydict = {line.split()[0]: line.split()[1] for line in myfile}
This transforms a file like
good buono
afternoon pomeriggo
thanks grazie
into
{"good": "buono", "afternoon": "pomeriggo", "thanks": "grazie"}
No need for tuples along the way.
.split() splits the input string on whitespace, removing any leading/trailing whitespace:
>>> " good\tbuono \n".split()
['good', 'buono']
with open('input.txt') as f:
result = map(str.split, f)
# -> [['good', 'buono'], ['afternoon', 'pomeriggo']]
d = dict(result)
# -> {'good': 'buono', 'afternoon': 'pomeriggo'}
ast will work as #onemach suggested, but you may want to read the strings in as tuples instead.
Snippet:
li = []
with open("file.txt", 'r') as f:
li.append(tuple(f.readline().split('\t')))
This will split the string about the tab, and place it into a tuple object, which is then appended into a list.

Categories