I am trying to import several text files into my Spyder file, which I want to add to a list later on.
why does
test1 = open("test1.txt")
result in test1 as "TextIOWrapper"? How would I bring the content over into the python file?
Thanks in advance
You need to read the lines into your list after opening it. For example, the code should be:
with open('test1.txt') as f:
test1= f.readlines()
The above code will read the contents of your text file into the list test1. However, if the data in your text file is separated over multiple lines, the escape char '\n' will be included in your list.
To avoid this, use the below refined code:
test1= [line.rstrip('\n') for line in open('test1.txt')]
Related
I am trying to remove some words (located on a text file) on another text file. Although my code seems to work, I noticed that it stopped removing words at a certain point in the text file. I then checked the text file, and noticed that Python is writing all the words on the same line, and this line has a limit of characters to it, resulting in the process stopping. How can I circumvent that?
Here is my code:
# text file list to array
with open('function_words.txt') as functionFile:
functionWords = [word for line in functionFile for word in line.split()]
# delete the word on the text file if it matches one of the array
with open("results.txt", "r+") as newfile:
newfile.write(' '.join(i for i in toPrint.read().split() if i not in functionWords))
Thanks in advance and please let me know if you need more details.
you would need to put the "\n" after you join the string if you want each line separate in the new file. Note the + "\n" below.
with open("results.txt", "r+") as newfile:
newfile.write(' '.join(i for i in toPrint.read().split() if i not in functionWords)+ "\n")
alt. you could create a list of the lines you want to write and write newFile using the writelines() methods. Something like:
newFile.writelines(my_list_of_lines_to_write)
I use the python script below to remove the first n lines of all text files in a folder.I want the deleted lines to be sent to a single output file.
Here is my code:
import glob
myfiles = glob.glob("*.txt")
for file in myfiles:
lines = open(file).readlines()
open(file, 'w').writelines(lines[4:])
I'm not providing a full written programming answer, because there's plenty of those to be found on stackexchange if you look there. Instead here's some hints to get you to where you hopefully need to be.
First, select the lines you want (to keep) from the file after you open it.
for myline in lines: # For each line, stored as myline,
# It's upto you to figure out how to only do the first 4...
mylines.append(myline) # mylines now contains, the lines you want.
open(removed_lines, 'a').writelines(mylines) # Save the 'removed lines'.
I'm using Notepad ++ to do a find and replacement function. Currently I have a a huge numbers of text files. I need to do a replacement for different string in different file. I want do it in batch. For example.
I have a folder that has the huge number of text file. I have another text file that has the strings for find and replace in order
Text1 Text1-corrected
Text2 Text2-corrected
I have a small script that do this replacement only for the opened files in Notepad++. For achieving this I'm using python script in Notepad++. The code is as follows.
with open('C:/replace.txt') as f:
for l in f:
s = l.split()
editor.replace(s[0], s[1])
In simple words, the find and replace function should fetch the input from a file.
Thanks in advance.
with open('replace.txt') as f:
replacements = [tuple(line.split()) for line in f]
for filename in filenames:
with open(filename, 'w') as f:
contents = f.read()
for old, new in replacements:
contents = contents.replace(old, new)
f.write(contents)
Read replacements into a list of tuples, then go through each file, and read the contents into memory, do the replacements, then write it back. I think the files get overwritten properly, but you might want to double check.
I am having trouble trying to figure out how to import text from a text file easily or at least a memorable method. I am trying to make a program that insults the user (for school), that brings in a word/s from one text file, adds another word/s from the second file and the final word/s from the third text file...
I am having trouble finding a way of coding to do this...I have the random number up and running to pick the text I just need to know how to access strings or text in a text file.
The easiest way is to use the with statement. It takes care of closing the file for you.
with open("file.txt") as f:
for line in f:
# do something with line
Or read the data into directly into a list:
with open("file.txt") as f:
lines = list(f)
# do something with lines
What I am trying to do here is :
1.read lines from a text file.
2.find lines that contain certain string.
3.delete that line and write the result in a new text file.
For instance, if I have a text like this:
Starting text
How are you?
Nice to meet you
That meat is rare
Shake your body
And if my certain string is 'are'
I want the output as:
Starting text
Nice to meet you
Shake your body
I don't want something like:
Starting text
Nice to meet you
Shake your body
I was trying something like this:
opentxt = open.('original.txt','w')
readtxt = opentxt.read()
result = readtxt.line.replace('are', '')
newtxt = open.('revised.txt','w')
newtxt.write(result)
newtxt.close()
But it don't seem to work...
Any suggestions? Any help would be great!
Thanks in advance.
Same as always. Open source file, open destination file, only copy lines that you want from the source file into the destination file, close both files, rename destination file to source file.
with open('data.txt') as f,open('out.txt') as f2:
for x in f:
if 'are' not in x:
f2.write(x.strip()+'\n') #strip the line first and then add a '\n',
#so now you'll not get a empty line between two lines