Home > OS >  How do I split a line from a file into a 2D list?
How do I split a line from a file into a 2D list?

Time:10-05

My file text.txt contains the following:

hello my
name is
jack black
and I
eat food

I'm trying to read this file into a 2D list called arr such that arr[0][0] = hello , arr[0][1] = my, and so on. I'm new to Python so I don't want to use numpy yet (I see that in a lot of similar questions).

Here is part of the code I have written so far:

for x in range(5):
    for y in range(2):
        nextLine = my_file.readline()
        nextLine = line.strip()
        arr[x][y]= [nextLine.split('\t')]
print(arr[0][0])
print(arr[0][1])

However, when I print arr[0][0] and arr[0][1]. I get the following:

[['hello my']]
[['name is']]

What is the best way to split 'hello' and 'my' such that each one enters the 2D list correctly in the same row?

CodePudding user response:

Don't use line counts. Just read until the file is empty. And .strip returns a list; you don't need to put it inside another list.

arr = []
for line in my_file:
    arr.append( nextLine.strip().split() )
print(arr[0][0])
print(arr[0][1])

CodePudding user response:

A file object produces lines when iterated over as a generator, so you can map the file object to the str.split method and construct a list with the resulting sequence:

arr = list(map(str.split, my_file))

CodePudding user response:

When we do

text=open("text.txt","r")
print(text.readlines())

We get the response

['hello my\n', 'name is\n', 'jack black\n', 'and I\n', 'eat food']

Since we are making a 2d array, our first index will be for the line and our second index will be for the specific word in the line.

text=open("text.txt","r")
wordArray=[]
allText=text.readlines()
for line in allText:
    lineArray=[]
    allWords=line.replace("\n","").split(" ")
    for word in allWords:
        lineArray.append(word)
    wordArray.append(lineArray)
print(wordArray)```

This will remove the \n's which cause a new line in the text file and then we split the words where there are spaces and append the words by line.
Hope this helps!

CodePudding user response:

with open("text.txt") as f:
    lines = f.readlines()
arr = [l.strip().split(" ") for l in lines]

CodePudding user response:

I would use pandas for reading the file and access the columns as arrays. Have a look into the pandas documentation for both operations: pd.read_csv() and df columns

df = pd.read_csv('input.txt', sep=" ", header=None, names=["a", "b"])
df["a"]  # This is your array
  • Related