Programmatically create a list of lists in Python 2.7 - python

I'm trying to create a list of lists by reading a file of data and I can't get it to work.
To show what I'm trying to do I have the following hard-coded list of lists that works for my purpose. Each list contains a room name, followed by an x and y coordinate. This is for a robot navigation program.
Here's the hard-coded listing that works.
nav_Array =[["studio",145.0,155.0],["kitchen",245.0,400.0]]
print (nav_Array[0])
print (nav_Array[1])
print ("\n")
print (nav_Array[0][0])
print (nav_Array[0][1])
print (nav_Array[0][2])
print ("\n")
print (nav_Array[1][0])
print (nav_Array[1][1])
print (nav_Array[1][2])
Here's the printed output I expected and got:
['studio', 145.0, 155.0]
['kitchen', 245.0, 400.0]
studio
145.0
155.0
kitchen
245.0
400.0
Now for my problem.
I have the following text file: I can modify the format of this file if need be, but I'd like to keep each entry on a new line for ease of maintenance. This is a very short excerpt of this file. The actual file is going to have many more lists. Each line being a list. By the way I've tried adding brackets and that doesn't help as you'll see further down:
studio,145.0,155.5
kitchen,245.0,400.0
So the problem is that I want to read this file into a list of lists like I had manually typed in as nav_Array in the previous example.
Here's what I tried.
First Attempt:
with open(file_path) as myfile:
for line in myfile:
line = line.rstrip()
print (line)
nav_Array.append(line)
myfile.close()
print "\n"
print "print nav_Array: ", nav_Array
print "\n"
print (nav_Array[0][0])
print (nav_Array[0][1])
print (nav_Array[0][2])
print "\n"
print (nav_Array[1][0])
print (nav_Array[1][1])
print (nav_Array[1][2])
The result is the following which is not what I want:
studio,145.0,155.5
kitchen,245.0,400.0
print nav_Array: ['studio,145.0,155.5', 'kitchen,245.0,400.0']
s
t
u
k
i
t
That's not creating a list of lists.
So then I tried adding brackets to the original file data like so:
[studio,145.0,155.5]
[kitchen,245.0,400.0]
Then running the same program as above I get the following output which is better, but still not a list of lists. The following is the output:
[studio,145.0,155.5]
[kitchen,245.0,400.0]
print nav_Array: ['[studio,145.0,155.5]', '[kitchen,245.0,400.0]']
[
s
t
[
k
i
For some reason single quotes are being put in by the append function. And that ruins what I'm trying to do. If I could get rid of those single quotes I'd be home free.
So then I tried the following to the original file format. All I did here was trying to add the [] in the append function itself, but this gives me precisely the same result as placing the [] in the data file.
The only line difference here is nav_Array.append("["+line+"]")
with open(file_path) as myfile:
for line in myfile:
line = line.rstrip()
print (line)
nav_Array.append("["+line+"]")
myfile.close()
print "\n"
print "print nav_Array: ", nav_Array
print "\n"
print (nav_Array[0][0])
print (nav_Array[0][1])
print (nav_Array[0][2])
print "\n"
print (nav_Array[1][0])
print (nav_Array[1][1])
print (nav_Array[1][2])
And this gives me the same output I got when I added the square brackets to the data file directly.
studio,145.0,155.5
kitchen,245.0,400.0
print nav_Array: ['[studio,145.0,155.5]', '[kitchen,245.0,400.0]']
[
s
t
[
k
i
Again that's not what I need. I need what I got when I typed in the list of lists by hand in the first example at the top of this post.
Just for the record, I've been trying tons of other things as well. I've been reading a lot of Stack Overflow posts on how to import lists from files, but I haven't been able to find anything that addresses bringing in an actual list of lists.
I'm almost there! I just can't get rid of the single quotes that appear to be introduced automatically by the append function.
I'm thinking I could write a program to go through and strip out those single quotes as a last resort. But surely there's a better way to build a list of lists without having to do that.
Thanks for reading.

Make use of splitlines and split:
s = """
studio,145.0,155.5
kitchen,245.0,400.0
"""
final = [i.split(',') for i in s.splitlines() if i]
print(final)
Output:
[['studio', '145.0', '155.5'], ['kitchen', '245.0', '400.0']]
Or for a file:
test.txt
studio,145.0,155.5
kitchen,245.0,400.0
You can use:
with open('test.txt') as f:
final = [i.split(',') for i in f.read().splitlines() if i]
print(final)
Output:
[['studio', '145.0', '155.5'], ['kitchen', '245.0', '400.0']]

The csv module works well with csv files. :)
import csv
with open('my.csv') as f:
reader = csv.reader(f)
a = list(reader)
print(a)
>>>
[['studio', '145.0', '155.5'], ['kitchen', '245.0', '400.0']]
>>>

Related

Find several strings in a .txt file, and assign them to a different variables

I have a SQL file, that I have converted to a txt file.
The final idea is to do a readline() with Python3 and find a specific string, and return only the lines that have this string.
For example, imagine this file.txt:
*hello, how are you
I am fine, --- thanks*/
What a wonderful day
Yes, it ---is
Regards---*
I need to print (or assign a variable/variables) only the lines where the string "---" appears:
*I am fine, --- thanks*/
Yes, it ---is
Regards---*
Thanks in advance.
Using list comprehension:
print ([line for line in open("test.txt").readlines() if "---" in line])
If you want the output in separate lines without "\n" and not list:
my_list = [line.strip() for line in open("test.txt").readlines() if "---" in line]
for line in my_list:
print(line)
line.strip() will remove "\n".

Python - Get specific characters from text file or from list

I have a text file with this in it
Curtain Open time: 8:00
When I wrote to the file I used this
File.write("Curtain Open Time: " + Var_CurtainOpenTime, + "\n")
I used the "\n" to go onto the next line for more data to be wrote. "Var_CurtainOpenTime" is a variable in this case it was "8:00". I have some code to read the line which looks like this:
FileRead = open('File.txt', 'r')
Printing this would read "Curtain Open Time: 8:00".
I want to be able to just get "8:00". I had previously used FileRead.split(" ") to separate each word but after the 8:00 I get ["Curtain", "Open", "Time:", "8:00\n"]. So I believe I would need to remove the first 3 indexes somehow and somehow remove '\n' from the last index. I don't know how I would approach this. Any help?
Try the following, I will comment the explain
with open('File.txt') as f:
[line.replace('\n','').split()[3:][0] for line in f][0]
or just:
FileRead = open('File.txt', 'r')
result = [line.replace('\n','').split()[3:][0] for line in FileRead][0]
you just need to change from the .split(" ") to .split() and then get the last list item
with open('file.txt') as f:
print f.read().split()[-1]
Well once you have the list from the split, you can remove the first 3 terms by doing l=l[3:] (where l is your list). Then you can remove the \n by doing s = s[:-1] where s is your desired string. This is using list slicing. You can look at documentation if you want to understand it further.

Next line escape character not working python

I used the following code to read from a text file line by line and print it on screen.
with open("source.txt") as f:
content = f.readlines()
print(content)
print('\n')
f.close()
But the \n was just getting appended to the output and the output was coming in a single line instead. For example if the file was like this:
abc
def
ghi
the output was:
['abc\n', 'def\n', 'ghi']
Then I tried changing the single quotes with the '\n' with "\n" like this:
with open("source.txt") as f:
content = f.readlines()
print(content)
print("\n")
f.close()
The actual output I need is:
abc
def
ghi
What can i do for that? Operating platform: Mac(Unix) Thanks in advance.
You should do it this way:
with open('source.txt', 'r') as f:
for line in f: #iterate over lines
line = line.strip() #removes whitespaces and new lines
print line #print the line, the print function adds new line
readlines() loads the whole file in memory and if the file is bigger than your memory you can't read it, so iterate over the file.
You can use rstrip():
>>> for i in content:
... print i.rstrip()
...
abc
def
ghi
The problem with your code is that it isn't doing what you would expect it to do. content is a list, and printing the list would just have ['abc\n', etc]. You can use a for-loop (as I have shown) to go through each element in the list and individually print out all the elements on a separate line.
I'm not exactly sure why you have print('\n'), but I'm presuming that you come from another programming language. Python automatically adds a newline, so adding one is not needed :).
Finally, rstrip() is needed to strip the newline, otherwise this would appear:
>>> for i in L:
... print i
...
abc
def
ghi
The problem is you were trying to print the list object itself, instead you should loop over the list and print individual items:
>>> lis = ['abc\n', 'def\n', 'ghi']
>>> print lis
['abc\n', 'def\n', 'ghi']
print lis actually prints the str representation of the list object:
>>> print str(lis)
['abc\n', 'def\n', 'ghi']
Loop over the list and print individual items. In python we can loop over the list itself unlike C/C++ where we require indexes.
>>> for item in lis:
... print item.rstrip('\n') #removes the trailing '\n'
...
abc
def
ghi
A for-loop over a list or any other iterable returns the next item from that iterable one by one and assigns it to the variable used in for-loop:
for x in lis: #in each iteration x is assgined the next item from lis
print x
with open('source.txt', 'r') as f:
content = f.read()
print content

Converting .txt file to list AND be able to index and print list line by line

I want to be able to read the file line by line and then when prompted (say user inputs 'background'), it returns lines 0:24 because those are the lines in the .txt that relate to his/her background.
def anaximander_background():
f = open('Anaximander.txt', 'r')
fList = []
fList = f.readlines()
fList = [item.strip('\n') for item in fList]
print(fList[:20])
This code prints me the list like:
['ANAXIMANDER', '', 'Anaximander was born in Miletus in 611 or 610 BCE.', ...]
I've tried a lot of different ways (for, if, and while loops) and tried the csv import.
The closest I've gotten was being able to have a print out akin to:
[ANAXIMANDER]
[]
[info]
and so on, depending on how many objects I retrieve from fList.
I really want it to print like the example I just showed but without the list brackets ([ ]).
Definitely can clarify if necessary.
Either loop over the list, or use str.join():
for line in fList[:20]:
print(line)
or
print('\n'.join(fList[:20])
The first print each element contained in the fList slice separately, the second joins the lines into a new string with \n newline characters between them before printing.
To print the first 20 lines from a file:
import sys
from itertools import islice
with open('Anaximander.txt') as file:
sys.stdout.writelines(islice(file, 20))

Parsing a tab-delimited text file to replace columns with one vertical list (Python)

I'm very new to Python and I know this is a pretty basic question. I have a text file with columns of data. I want to remove the columns and make it one long list.
I have the following code:
for line in open('feddocs_2011.txt', 'r'):
segmentedLine = line.split("/t")
print segmentedLine
This seems to create a separate string for each line, but I think I may need to loop through each of those new strings to split those next. I thought it would have put everything following a tab on a new line. I tried the following, but got an error message that "list" doesn't have a split function.
while segmentedLine:
item = segmentedLine.split("\t")
print item
Thanks very much for any input.
You've got the lines split properly in the first loop. What you want to do then is have a second for loop to iterate over each tab-separated item. That'll look like this:
for line in open('feddocs_2011.txt', 'r'):
segmentedLine = line.split("\t")
for item in segmentedLine:
print item
Or more concisely, without the temporary variable:
for line in open('feddocs_2011.txt', 'r'):
for item in line.split("\t"):
print item
what about:
x = [line.split('\t') for line in open('file.txt')]
and you can join the lists, if you want:
sum(x, [])
[Edit]
if your file only have tabs (no spaces) you can simply do:
x = open('file.txt').read().split()
So you have
foo<tab>bar<tab>baz
bla<tab>bla<tab>bla
and you want it to be
foo
bar
baz
bla
bla
bla
Right?
Then you can just do
with open("myfile.txt", "r") as f:
text = f.read().replace("\t", "\n")
Now text is a single string. If you want a list of all the items instead (["foo", "bar", "baz", "bla", "bla", "bla"]), use
text = f.read().replace("\t", "\n").split("\n")
if I understand correctly, what you're after is:
import itertools
print '\n'.join(list(itertools.chain(*[line.strip().split(',') for line in open('feddocs_2011.txt', 'r')])))
put everything following a tab on a new line
If this is all you want, why not just use the str.replace function?
for line in open('feddocs_2011.txt', 'r'):
segemented_line = line.replace('\t', '\n')
print(segmented_line)
If, for some reason, you want to keep the tabs:
for line in open('feddocs_2011.txt', 'r'):
segemented_line = line.replace('\t', '\t\n')
print(segmented_line)

Categories

Resources