How to get text from different file (.txt) as output in python code | Sololearn: Learn to code for FREE!

+2

How to get text from different file (.txt) as output in python code

Does anyone know how to get the text from a txt file to show as output in my python code. The content of the text file is in list format and I want to use it as a list in my python code. Ex- Text file [1,2,3,4] Python code to take content of text file as output 1st element of list is 1 2nd element of list is 2 Elements greater than 1 are 2,3,4 [ Problem is solved now ✌🏻 ]

6/30/2022 8:20:02 AM

Aman Singh

12 Answers

New Answer

+3

Data = open("file_name.txt", "r") (this r specific's reading of file) data = [] data.extend(Data) Now data contains all values of text file in list format.

+6

Aman Singh , i have followed all the posts / answers to your question. this was: how to get a "list" from a text file, so that you can use it. => finally after 2 days you posted: ... but in reality my file text not only contains digits but also some hexadecimal IP address ... it would have helped all of the people that have put some time and effort in answering you, if these "new facts" would have been given from the start. sorry to say but this behaviour is unreasonable. It's hard for me to imagine helping you answer a question again.

+5

Satyam Mishra , your description is not completely correct. all information in a file that is created as a txt file, is in string data format (not in list format), that gives a result as (by using your code): ['[1,2,3,4,5]'] this is a list with a string inside, the inner squared brackets are also part of the string. before using the list, it has to be cleansed and the elements has to be converted to integers. this is not a problem with using a list. but what should we do when the "data" from the file is meant as a nested list or a dictionary? i would prefer using a data format like json or pickle for storing python objects. creating the files and reading the content back is easy because the python object will be kept by storing and reading.

+5

Aman Singh , when reading the "list" from the file, you will get a string: "[1,2,3,4]" (please keep in mind the numbers can consist of more than one digit). to get the elements from this string, you can: -> remove the enclosing brackets, then split the remaining string to a comma as separator -> a for loop that reads the numbers (still in string format), convert them to int and append them to a new list -> an other way that is more flexible and also easy to handle is using a regular expression. a pattern like "\d+" combined with the findall() function does extract all numbers from the string. we do not need to remove the brackets first, and no split is required. only a conversion from string to int for the numbers has to be performed.

+3

Satyam Mishra thanks for the suggestion but in reality my file text not only contains digits but also some hexadecimal IP address so I am thinking to use isalpha() with isdigit(). Thanks for your help.

+2

I didn't get your question!

+1

Lothar the text file just contains [1,2,3,4] I want to access the elements separately. The above solution code creates a nested list, all that left for me is to figure how to access it properly in a loop. ✌🏻

+1

You can try this with open ("file.txt","r") as d: data = d.readLine() Here data variable should be like "[1,2,3,4]" Then we can iterate each character and check whether it's int or not lis = [] for i in data: if (i.isdigit()): lis.append(int(i))

+1

You ask for python but have c and cpp tags 💀

+1

Lothar Hey sorry for the inconvenience but the problem was solved 2 days ago as Satyam Mishra provided a solution for my case and I just wanted a rough idea to help me with my project so I thought it wasn't worth making the question complicated. Anyway thanks for the efforts you put into finding the answer. I really appreciate it and I'll try to give full details of the problem from now on. 🙇🏻

0

Satyam Mishra wat other methods does data have that r interesting? just list? cooool

0

Ohh yeah, thanks for correction.