-3

I have a .txt file that was saved with python. It has the form:

file_inputs

Where the first line is just a title that helps me remember the order of each element that was saved and the second line is a sequence of a string ('eos') and other elements inside. How can I call the elements so that inputs[0] returns a string ('eos') and inputs[1] returns the number "5", for example?

Ramos
  • 9
  • 4
  • 1
    [Why should I not upload images of ... when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question). [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). [Why not upload images of code on SO ...?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). [You should not post code as an image because:...](https://meta.stackoverflow.com/a/285557/2823755). – wwii Nov 16 '22 at 01:17
  • use basic python text file read. Read a line, split it (on comas), convert the relevant strings to numbers. This should all be covered in your python reference book or tutorial. – hpaulj Nov 16 '22 at 01:22

2 Answers2

0

I am not sure why you want inputs[&] to return 5.

However here is a the standard (simple) way to read a text file with python:

f = open('/path/to/file.txt', 'r')
content = f. read()
#do whatever you want there
f. close()

To get eos printed first you might want to iterate through the content string until you find a space. For the 5 idk.

GaetanG
  • 1
  • 2
  • Hi, thanks! I tried that way, but it reads the title. I don't want to read the title, just the elements below and be able to call them in the form ```inputs[i]``` – Ramos Nov 16 '22 at 01:42
  • Ok, you have to do that yourself. What you want to do is to "reject" the first line (which is your title if I understood correctly). The exact same question is discussed here : https://stackoverflow.com/questions/4796764/read-file-from-line-2-or-skip-header-row (make sure to use google and stackoverflow to find the answer to your questions before posting about it) Apparently this works: f = open(fname,'r') lines = f.readlines()[1:] f.close() – GaetanG Nov 17 '22 at 10:53
  • Regarding the "input" part, you basicaly want to turn your string into an array, once again you can just google how to do that like "python turn string into array". Here isn a link that should answer your question: https://appdividend.com/2020/09/25/how-to-convert-python-string-to-array/ – GaetanG Nov 17 '22 at 10:56
0

if i could understand, you will have to do something like this

input = open(<file_name>, 'r')
input = input.readlines()
input.pop(0) #to remove the title str
#now you can have an array in wich line of .txt file is a str
new_input = [None]*len(input)
for index, line in enumerate(input):
  new_input[index] = line.split(",") #with this your input should be an array of arrays in wich element is a line of your .txt with all your elements
#in the end you should be able to call
input[0][1] #first line second element if i didnt mess up this should be 5
Thales
  • 59
  • 5