Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python convert file into list

# Saving the list
a = ["apple", "banana", 2, 3]
with open("test.txt", "w") as f:
    for item in a:
        f.write("%s
" % item)

with open("test.txt", "r") as f:
    print(f.read())

# Converting contents of file into new list
f = open("test.txt", "r")
listItems = f.read().splitlines()
print(listItems)
Comment

python file to list

import pandas as pd

def file_to_list(file):
    rtn: object = []
    file_object: object = open(file, "r")
    rtn: object = file_object.read().splitlines()
    file_object.close()
    return list(filter(None, pd.unique(rtn).tolist())) # Remove Empty/Duplicates Values
    pass

# Example #    
data_from_file: object = file_to_list('filename.txt')    
Comment

PREVIOUS NEXT
Code Example
Python :: django get query parameters 
Python :: Video to text convertor in python 
Python :: Python RegEx Escape – re.escape() 
Python :: make poetry env 
Python :: python count character occurrences 
Python :: How To Get Redirection URL In Python 
Python :: how to convert csv into list 
Python :: delete column in dataframe pandas 
Python :: python to float 
Python :: extract text from pdf python 
Python :: pandas df sample 
Python :: list comprehension python if else 
Python :: conda install pypy 
Python :: how to run python file from cmd 
Python :: random.sample python 
Python :: django insert template in another template 
Python :: pandas row sum 
Python :: df astype 
Python :: python print n numbers 
Python :: install django in windows 
Python :: python elapsed time module 
Python :: read clipboard python 
Python :: append write python 
Python :: install glob module in linux 
Python :: how to check any script is running in background linux using python 
Python :: get country from city python 
Python :: validate ip address 
Python :: python inject into process 
Python :: use map in python to take input 
Python :: hashmap python 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =