3

i am learning python programming and am stuck with this problem.I looked into the other examples which reads the file input and makes the entire thing as a single list or as a string link to that example but i want each line to be a list(nested lists) how do i do it please help
The text file is a.txt

1234 456 789 10 11 12 13

4456 585 568 2 11 13 15 

the output of the code must be like this

[ [1234 456 789 10 11 12 13],[4456 585 568 2 11 13 15] ]
Community
  • 1
  • 1
sharath
  • 55
  • 6
  • 3
    The absence of both quotes and commas in the "must be like this" required output makes that output impossible to obtain by normal `print`s and would require contortions. I suspect you just omitted quotes, commas, or both, in that "must be", so please edit the Q to clarify! – Alex Martelli Feb 06 '15 at 19:01

4 Answers4

5

No reason to do readlines -- just iterate over the file.

with open('path/to/file.txt') as f:
    result = [line.split() for line in f]

If you want a list of lists of ints:

with open('path/to/file.txt') as f:
    result = [map(int, line.split()) for line in f]
    # [list(map(int, line.split())) for line in f] in Python3
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
2

You can do

with open('a.txt') as f:
     [[i.strip()] for i in f.readlines()]

It will print

[['1234 456 789 10 11 12 13'], ['4456 585 568 2 11 13 15']]

Note - This is an answer to your initial problem to print strings

To print exactly as you want without quotes, this is a very bad approach

print(repr([[i.strip()] for i in f.readlines()]).replace("'",''))

which will print

[[1234 456 789 10 11 12 13], [4456 585 568 2 11 13 15]]
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
2
lines = open('file.txt').readlines() # parse file by lines
lines = [i.strip().split(' ') for i in lines] # remove newlines, split spaces
lines = [[int(i) for i in j] for j in lines] # cast to integers
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • 1
    This has the unintended side effect of leaving an open file handler. Not too big a deal for a small program but not a great habit to get into – Adam Smith Feb 06 '15 at 18:59
  • 1
    There's no need to close because we're simply reading not writing. – Malik Brahimi Feb 06 '15 at 19:00
  • 2
    Except you have an file handler object with an open connection so the garbage collector can't free it. This is how memory leaks are made on big projects, someone saying "Well I don't ever write to this file, so no need to worry about closing it...." As I said: never going to matter in this scenario, but it ***is*** a bad habit. – Adam Smith Feb 06 '15 at 19:01
  • 1
    @AdamSmith YAY! Get into good habits as, well, a habit. "Don't need to close this file since I'm only reading" is a bit like saying "don't need to brush my teeth tonight since I just had broth" or "don't need to stop at this particular stop sign since there's nobody else around anyway", etc, etc. Close the files you open, brush your teeth regularly, stop at every stop sign, etc, etc -- **good habits**, man, go for them, everywhere, all the time!-) – Alex Martelli Feb 06 '15 at 19:07
2

Looks like you want integers, not strings, in the resulting lists; if so:

with open(filename) as f:
    result = [[int(x) for x in line.strip().split()] for line in f]
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    @KasraAD, efficiency's a marginal consideration here (I/O time dominates anyway) and might go either way -- my motivation is that listcomps are more general and readable so should usually be preferred unless there are specific reasons making `map`, `filter`, &c, preferable in a given case. Together with genexps (and dict and set comprehensions) listcomps make up the best set of features Python copied from Haskell (with more readable syntax, as is Python's wont:-), though Haskell's elegant lazy evaluation semantics mean they get away with a single such concepts while we need a few:-). – Alex Martelli Feb 06 '15 at 19:13