I've a text file with many lines. I need to append to every line a text in Python.
Here an example:
Text before:
car
house
blog
Text modified:
car: [word]
house: [word]
blog: [word]
If you just want to append word on each line this works fine
file_name = 'YOUR_FILE_NAME.txt' #Put here your file
with open(file_name,'r') as fnr:
text = fnr.readlines()
text = "".join([line.strip() + ': [word]\n' for line in text])
with open(file_name,'w') as fnw:
fnw.write(text)
But there are many ways to do it
Read the text in a list:
f = open("filename.dat")
lines = f.readlines()
f.close()
append text:
new_lines = [x.strip() + "text_to_append" for x in lines]
# removes newlines from the elements of the list, appends
# the text for each element of the list in a list comprehension
Edit:
for completness, a more pythonic solution with writing the text to a new file:
with open('filename.dat') as f:
lines = f.readlines()
new_lines = [''.join([x.strip(), text_to_append, '\n']) for x in lines]
with open('filename_new.dat', 'w') as f:
f.writelines(new_lines)
Related
Link to example image(This is the approach that I tried)
with open(directory) as f: # Loop to parse through each FASTA file
line = f.readline()
while line:
line= f.readline()
data = line.split('\n\n')
data = [line.replace('\n', ' ') for line in data]
You have to open the file (lets say "fasta.txt"), not the directory. I suggest you to adapt your solution this way:
with open("fasta.txt") as f:
content = f.read().split("\n\n")
data = [seq.replace("\n", "") for seq in content]
I have some words in a text file like:
joynal
abedin
rahim
mohammad
joynal
abedin
mohammad
kudds
I want to delete the duplicate names. It will delete these duplicate entries totally from the text file
The output should be like:
rahim
kuddus
I have tried some coding but it's only giving me the duplicate values as one like 1.joynal and 2.abedin.
Edited: This is the code I tried:
content = open('file.txt' , 'r').readlines()
content_set = set(content)
cleandata = open('data.txt' , 'w')
for line in content_set:
cleandata.write(line)
Use a Counter:
from collections import Counter
with open(fn) as f:
cntr=Counter(w.strip() for w in f)
Then just print the words with a count of 1:
>>> print('\n'.join(w for w,cnt in cntr.items() if cnt==1))
rahim
kudds
Or do it the 'old fashion way' with a dict as a counter:
cntr={}
with open(fn) as f:
for line in f:
k=line.strip()
cntr[k]=cntr.get(k, 0)+1
>>> print('\n'.join(w for w,cnt in cntr.items() if cnt==1))
# same
If you want to output to a new file:
with open(new_file, 'w') as f_out:
f_out.write('\n'.join(w for w,cnt in cntr.items() if cnt==1))
you can just create a list which appends if name is not in and remove if name is in and occured a 2nd time.
with open("file1.txt", "r") as f, open("output_file.txt", "w") as g:
output_list = []
for line in f:
word = line.strip()
if not word in output_list:
output_list.append(word)
else:
output_list.remove(word)
g.write("\n".join(output_list))
print(output_list)
['rahim', 'kudds']
#in the text it is for each row one name like this:
rahim
kudds
The solution with counter is still the more elegant way imo
For completeness, if you don't care about order:
with open(fn) as f:
words = set(x.strip() for x in f)
with open(new_fn, "w") as f:
f.write("\n".join(words))
Where fn is the file you want to read from, and new_fn the file you want to write to.
In general for uniqueness think set---remembering that order is not gauranteed.
file = open("yourFile.txt") # open file
text = file.read() # returns content of the file
file.close()
wordList = text.split() # creates list of every word
wordList = list(dict.fromkeys(wordList)) # removes duplicate elements
str = ""
for word in wordList:
str += word
str += " " # creates a string that contains every word
file = open("yourFile.txt", "w")
file.write(str) # writes the new string in the file
file.close()
The file is called "emotion_words" which I want the first word of each line for.
I want to use a nested for loop, but I am not sure how.
Would I do this
emotions=open("emotion_words.txt","r+")
content = emotions.read()
for line in content.split(' ',1):
And add an append function before the second for loop?
with open("emotion_words.txt","r+") as f:
for line in f:
first_word_in_line = line.split(" ")[0]
fileref = open ("emotion_words.txt","r")
line = fileref.readlines()
emotions = []
for words in line:
word = words.split()
emotions.append(word[0])
print (emotions)
If I understand you question correctly, this should work for you:
words = []
emotions = open("emotion_words.txt", "r+")
for l in emotions:
first_word = l.split()[0]
words.append(first_word)
After that you have your words in a 'words' list.
Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.
For example;
input.txt contains different lines with text
Then I use this to convert to list:
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
Then outputting the list into a text file not like ['string', 'string'] but on new lines.
Any help with this???
$ cat input.txt
HELLO
WORLD
In python you can use '\n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = '\n'.join(content)
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%s\n" % item)
You can create a string like this:
elements_on_new_line = '\n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
f = open (FilePath, "r")
#print f
with open(FilePath, "r") as f:
lines = f.readlines()
#print lines
for iterms in lines:
new_file = iterms[::-1]
print new_file
it gives me a result like this:
7340.12,8796.4871825,0529.710635,751803.0,fit.69-81-63-40tuo
original list is like this:
out04-32-45-95.tif,0.330693,536043.5237,5281852.0362,20.2260
it is supposed to be like this:
20.2260, ...........out04-32-45-95.tif
You should be using your for loop like:
for iterms in lines:
new_file = ','.join(iterms.split(',')[::-1])
print new_file
Explanation:
In your current code, the line iterms[::-1] reverses the entire string present in your line. But you want to only reverse the words separated by ,.
Hence, you need to follow below steps:
Split the words based on , and get list of words:
word_list = iterms.split(',')
Reverse the words in the list:
reversed_word_list = word_list[::-1]
Join the reversed wordlist with ,
new_line = ','.join(reversed_word_list)