-3

I am trying to open a file and then take that file and turn it into a list I'm kinda lost as to how to get it into a list i know I can open a file with open() I don't want to use the read.line either

Input (build1,200),(build2,267) all in a txt file that needs to be opened

Output

Build1,200

Build2,200

Every time I try to add the info to a list it just adds the first one then it stops .

2 Answers2

1

This will put the each line into separate sub lists in a 2d list:

sav = []
with open("filename", "r") as fileopen:
    for line in fileopen:
        sav.append(line.split())

I'm assuming you are using a .txt file.

Will
  • 4,585
  • 1
  • 26
  • 48
zeldor
  • 101
  • 1
  • 9
0

This is basically going to make an sequence named 'tup'. What open() does is open up the file. The two arguments that you pass will be the 'filename' and the what you want to do with the contents of the file. Filename is going to be the entire directory of the file, i.e "C:/User...../file.txt". What 'r' signify is 'read' a file only. tuple() will create a sequence of data from your file which will be immutable (you cannot change it), but you can access the data within it.

    tup=tuple(open(file,'r'))  
Rogue
  • 56
  • 1
  • 9
  • I found a similar question from a while ago, this might help also. http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array – Rogue Mar 09 '15 at 23:07