How to concatenate in Python from .txt? - python-2.7

I have a server.txt file where i have 3 names listed down:
server.txt
CFMPAPP1
CFMPAPP2
CFMPAPP3
i am looking to take these names by calling that server.txt file and want the output.txt file as mentioned below.
output.txt
CI_Name like 'CFMPAPP1%' or
CI_Name like 'CFMPAPP2%' or
CI_Name like 'CFMPAPP3%' or
Any Idea how to do this ?

This can be easily done in three lines:
with open('server.txt', 'r') as file:
s = "".join(file.read())
amended_string = "\n".join([ "CI_Name like '{}%' or".format(a) for a in s.split('\n')])
And then you just need to save amended_string to output.txt. I hope that helps.

This solution only keeps one line at a time in memory:
with open('server.txt', 'r') as infile:
with open('output.txt', 'w') as outfile:
for line in infile:
outfile.write("CI_Name like '{}%' or\n".format(line.rstrip()))

Related

extracting data from tow different files to produce a fasta file

I have two different files one is a fasta file, and the other a txt file produced from a dictionary with json.
file_A looks like this;
> {
"gene_1005 ['gene description_B']":2,
"gene_1009 ['gene description_C']":1,
"gene_104 ['gene description_D']":2,
"gene_1046 ['gene description_A']":1,
}
file_B looks like this:
gene_1005 ['gen description_B'] ATGTGGATCCGCCCGTTGCAGGCGGAACTGAGCGATAACACGCTGGCTTTGTATGCGCCAAACCGTTTTGTGCTCGA
gene_2 ['gene description_C'] ATGAAATTTACCGTTGAACGTGAACATTTATTAAAACCGCTGCAACAGGTGAGTGGCCCATTAGGTGGCCGCCCAAC
what I would like to create is a new fasta file only containing those genes that have the value 2 in the file_A. I have tried the code below but I am quite lost. It will print the word[0], that is the name of the gene but it will not print word[1], that should be the number. It sends the error
'out of range'
import json
def readlines():
input_file=open('file_A.txt')
lines=input_file.readlines()
print lines[1]
for line in lines:
words=lines.split(':')
print words[0]
print words[1]
#print line
input_file.close()
readlines()
Could anyone kindly give a hand with this, please?
Thanks
I see people like giving negative without explaining why or giving an suggestion, and that was the suggestion of this post. But as I see that the negative-voter has not bother with a suggestion, I will post the answer to it.
input_file= open('file.fa', 'r')
output_file= open(wanted_genes.fa', 'w')
for line in input_file:
if line[0]=='>':
geneID=line[1:-1]
if geneID in my_dict:
output_file.write(line)
skip=0
else:
skip=1
else:
if not skip:
output_file.write(line)
input_file.close()
output_file.close()

How best to display the content of a text file in python

Ok, I am abit of a python beginner. So, forgive me if this question sounds silly.
I have a directory that contains some .txt files as shown in the image below:
The 1.txt file contains :
Lo! I am lost.
I want to write a programme that goes through each file in the shakespeare directory and print out the content of the .txt file. Below is a programme I have written but I am not sure how to print out the content of each file. all it prints is the name of each file but how do I really print out the content of each file.
def readFromCorpus(path):
os.chdir(path)
for fu in glob.glob("*.txt"):
print fu
readFromCorpus('./trainingData/shakespeare')
I am sorry if this is really a silly question. I just need a pointer to what I am doing wrong.
Thanks
Try this:
def readFromCorpus(path):
os.chdir(path)
for fu in glob.glob("*.txt"):
print('\n\n'+fu)
with open(fu,'r') as f:
data = f.readlines()
for line in data:
print(line.replace('\n',''))

Python read and write in same function

My code is currently taking in a csv file and outputting to text file. The piece of code I have below and am having trouble with is from the csv I am searching for a keyword like issues and every row that has that word I want to output that to a text file. Currently, I have it printing to a JSON file but its all on one line like this
"something,something1,something2,something3,something4,something5,something6,something7\r\n""something,something1,something2,something3,something4,something5,something6,something7\r\n"
But i want it to print out like this:
"something,something1,something2,something3,something4,something5,something6,something7"
"something,something1,something2,something3,something4,something5,something6,something7"
Here is the code I have so far:
def search(self, filename):
with open(filename, 'rb') as searchfile, open("weekly_test.txt", 'w') as text_file:
for line in searchfile:
if 'PBI 43125' in line:
#print (line)
json.dump(line, text_file, sort_keys=True, indent = 4)
So again I just need a little guidance on how to get my json file to be formatted the way I want.
Just replace print line with print >>file, line
def search(self, filename):
with open('test.csv', 'r') as searchfile, open('weekly_test.txt', 'w') as search_results_file:
for line in searchfile:
if 'issue' in line:
print >>search_results_file, line
# At this point, both the files will be closed automatically

Python: copy line, conditional criteria

I have been searching for following Python solution to copy selectively lines from 1 txt file to another. I can copy the whole file, but with only a few lines I get an error.
My code:
f = open(from_file, "r")
g = open(to_file, "w")
#copy = open(to_file, "w") # this instruction copies whole file
rowcond2 = 'xxxx' # look for this string sequence in every line
for line in f:
if rowcond2 in f:
copy.write(line,"w") in g # write every corresponding line to destination
f.close()
# copy.close() # code receive error to close destination
g.close()
So without the rowcond2, I can copy the whole file. Yet with the condition nothing is written to destination file.
Thank you for your help.
Why not to put your condition inside the for loop?
for line in f:
if condition:
copy.write(line)
I have been able to solve this case searching on SO:
Using python to write specific lines from one file to another file
#Lukas Graf: thank you for your detailed step wise explanation.

Formatting text file

I have a txt file that I would like to alter so I will be able to place the data into columns see example below. The reason behind this is so I can import this data into a database / array and perform calculations on them. I tried importing/pasting the data into LibreCalc but it just imports everything into one column or it opens the file in LibreWriter I'm using ubuntu 10.04. Any ideas? I'm willing to use another program to work around this issue. I could also work with a comma delimited file but I'm not to sure how to convert the data to that format automatically.
Trying to get this:
WAVELENGTH, WAVENUMBER, INTENSITY, CLASSIFICATION, CODE,
1132.8322, 88274.326, 2300, PT II, 9356- 97630, 05,
Here's a link to the full file.
pt.txt file
Try this:
sed -e "s/(\s+)/,$1/g" pt.txt
is this what you want?
awk 'BEGIN{OFS=","}NF>1{$1=$1;print}' pt.txt
if you want the output format looks better, and you have "column" installed, you can try this too:
awk 'BEGIN{OFS=", "}NF>1{$1=$1;print}' pt.txt|column -t
The awk and sed one-liners are cool, but I expect you'll end up needing to do more than simply splitting up the file. If you do, and if you have access to Python 2.7, the following little script will get you going.
# -*- coding: utf-8 -*-
"""Convert to comma-delimited"""
import csv
from os import path
import re
import sys
def splitline(line):
return re.split('\s{2,}', line)
def main():
srcpath = path.abspath(sys.argv[1])
targetpath = path.splitext(srcpath)[0] + '.csv'
with open(srcpath) as infile, open(targetpath, 'w') as outfile:
writer = csv.writer(outfile)
for line in infile:
if line.startswith(' '):
line = line.strip()
cols = splitline(line)
writer.writerow(cols)
if __name__ == '__main__':
main()
The easiest way turned out to be importing using a fixed width like tohuwawohu suggested
Thanks
Without transforming it to a comma-separated file, you could access the csv import options by simply changing the file extension to .csv (maybe you should remove the "header" part manually, so that only the columns heads and the data rows do remain). After that, you can try to use whitespace as column delimiter, or even easier: select "fixed width" and set the columns manually. – tohuwawohu Oct 20 at 9:23