get text from txt file python
with open ("data.txt", "r") as myfile:
data = myfile.read().splitlines()
get text from txt file python
with open ("data.txt", "r") as myfile:
data = myfile.read().splitlines()
read a text file in python
f = open("welcome.txt", "r")
print(f.read())
f.close()
read text file in python
file = '/home/text/chapter001.txt'
f=open(file,'r')
data = f.read()
print('data =',data)
how to read text frome another file pythion
f = open("Myfile.txt", "r")
print(f.read()) #this is it if your using visual studio code.
# it may work on other things but try it.
#here is the other bit of code for the other file
#-------------------------------------------------------------------------
#this will overwrite anything in the text file!!!
when you run the code
file1 = open("MyFile.txt","w+") #this is the code to add text to the file
file1.readline()
file1.write("boy") #where you type in to add text to fiel
file1.close() # to close the file
parsing text in python
my_string = 'Names: Romeo, Juliet'
# split the string at ':'
step_0 = my_string.split(':')
# get the first slice of the list
step_1 = step_0[1]
# split the string at ','
step_2 = step_1.split(',')
# strip leading and trailing edge spaces of each item of the list
step_3 = [name.strip() for name in step_2]
# do all the above operations in one go
one_go = [name.strip() for name in my_string.split(':')[1].split(',')]
for idx, item in enumerate([step_0, step_1, step_2, step_3]):
print("Step {}: {}".format(idx, item))
print("Final result in one go: {}".format(one_go))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us