0

So I understand how to manipulate a text file and move data in and out of the program, but I'm trying to take raw data in a text file, and load them into an array that is originally empty, how would I make this approach?

Assume my raw data contains 3 words, I want to place those words into a variable called Array. The raw data of the text file contains the 3 following words: ' Apple Banana Orange '. I would like it to load into the array as: Array = ["Apple", "Banana", "Orange"]. How would you approach this?

with open("C:\\Users\\NameList.txt","r") as f:
    Array = []
    nameList = f.readlines(Array)

Am aware the code is wrong, but I'm not sure how to fix even after reading so much.

5
  • import numpy as np my_list = [2,4,6,8,10] my_array = np.array(my_list) # printing my_array print my_array # printing the type of my_array print type(my_array) copied from educative.io/edpresso/… Commented Feb 12, 2021 at 17:58
  • @pippo1980 I guess, the OP uses array as a synonym of list, they are not referring to a numpy array. Commented Feb 12, 2021 at 17:59
  • 1
    Array = open("C:\\Users\\NameList.txt", "r").readlines() ? Commented Feb 12, 2021 at 18:01
  • One word per line or all words in one line? If first then use Array = f.readlines(). Commented Feb 12, 2021 at 18:02
  • from text is r all words in one line: The raw data of the text file contains the 3 following words: ' Apple Banana Orange ' Commented Feb 12, 2021 at 18:08

2 Answers 2

1

If your input test.txt is like below:

Apple Banana Orange

This is the solution you are looking for.

with open("test.txt","r")as f:
    text = f.readlines()
    Array = text[0].split()

In case you have more than 1 line, you can use this one:

with open("test.txt","r")as f:
    text = f.read().splitlines()
    Array = [i.split() for i in text]
Sign up to request clarification or add additional context in comments.

Comments

0

This will read all the lines in your file:

with open("C:\\Users\\NameList.txt","r")as f:
    lines = f.read().splitlines()
Array = list()

for line in lines:
    Array.append(line)
print(Array)

for item in Array:
    if 'Apple' == item:
        print(item)

Output:

#first loop
['Apple', 'Banana', 'Orange']
#second loop
Apple

6 Comments

Ok, and they are listed as strings? because i would like to be able to access each individual as a string, and if i run a condition to confirm a variable like name = "Apple" and find out if its in the array, it doesnt seem to detect it
dont know the way to set array type in array but there should be one
They are string, you can use a print(type(item)) to check the type, I edited the code for what you need
stackoverflow.com/questions/58882019/… ; Can I store strings in python array try
does: 'Array = Array.tounicode()' works ? see : docs.python.org/3/library/array.htmlhttps://docs.python.org/3/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.