How can I print a single row element in Python DictReader - python

I've got a program to find DNA matches. We've been given a one line text file for finding the longest sequence of STR(Short Tandem Repeats) then match the result with a database which has been a .cvs file as below:
> name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
> Albus,15,49,38,5,14,44,14,12
> Draco,9,13,8,26,15,25,41,39
> Ginny,37,47,10,23,5,48,28,23
> Harry,46,49,48,29,15,5,28,40
After getting results for longest sequence amounts(as integer) I'm trying to find a match in this cvs file and print the value in the first column (name) when I found the matching numbers of STR. I'm checking the program with print() and it prints 'no match' until the matched name then stops with below error code:
Traceback (most recent call last):
File "dna.py", line 37, in <module>
main()
File "dna.py", line 27, in main
print(row[0])
KeyError: 0
My program probably finds the match but can't print out than exits. Could you please help me? TIA
def main():
for i in SEQ.keys():
SEQ[i] = find_longest_sequence(i)
print(SEQ.items())
with open(sys.argv[1],'r') as f:
db = csv.DictReader(f)
for row in db:
if int(row['AGATC']) == SEQ['AGATC'] and int(row['AATG']) == SEQ['AATG'] and int(row['TATC']) == SEQ['TATC']:
print(row[0])
else:
print("No match")

I used the data you provided and made a test.csv
name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
Albus,15,49,38,5,14,44,14,12
Draco,9,13,8,26,15,25,41,39
Ginny,37,47,10,23,5,48,28,23
Harry,46,49,48,29,15,5,28,40
Then tested with the folowing code (py csv-test.py test.csv):
import csv
import sys
with open(sys.argv[1], "r") as f:
db = csv.DictReader(f)
for row in db:
if int(row['AGATC']) == 15:
print(row['name'])
And the result was "Albus".
PS. With:
print(row[0])
I get the same KeyError as you do.
Do I miss something?

Related

Replace String from file In python?

I have a file that contains several Phone Number.
Now I want to convert any line of this file to VCF file.
So,first i defined e template model for VCF file that have a String "THISNUMBER"
And i want to open file (thats have phone numbers) and replace thats lines to Template model (THISNUMBER)
i write this Python code :
template = """BEGIN:VCARD
VERSION:3.0
N:THISNUMBER;;;
FN:THISNUMBER
TEL;TYPE=CELL:THISNUM
END:VCARD"""
inputfile=open('D:/xxx/lst.txt','r')
counter=1
for thisnumber in inputfile:
thisnumber=thisnumber.rstrip()
output=template.replace('THISNUMBER',thisnumber)
outputFile=('D:/xxx/vcfs/%05i.vcf' % counter,'w')
outputFile.write(output)
output.close
print ("writing file %i") % counter
counter +=1
inputfile.close()
But I Give This ERROR :
Traceback (most recent call last):
File "D:\xxx\a.py", line 16, in <module>
outputFile.write(output)
AttributeError: 'tuple' object has no attribute 'write'
I'll write a full fledged answer because I want to address your code style, if that's fine.
The problem is likely that you forgot to call open() on your outputFile. But let me introduce to you a nice way of handling files in Python. This way you don't even have to remember to call close(). It is all done with a context manager. The file is closed when the with statement exits.
template = """BEGIN:VCARD
VERSION:3.0
N:THISNUMBER;;;
FN:THISNUMBER
TEL;TYPE=CELL:THISNUM
END:VCARD"""
with open('D:/xxx/lst.txt', 'r') as inputfile:
counter = 1
for number in inputfile:
number = number.rstrip()
output = template.replace('THISNUMBER', number)
with open('D:/xxx/vcfs/%05i.vcf' % counter, 'w') as outputFile:
outputFile.write(output)
print('writing file %i' % counter)
counter += 1
change
outputFile=('D:/xxx/vcfs/%05i.vcf' % counter,'w')
to
outputFile=open('D:/xxx/vcfs/%05i.vcf' % counter,'w')

Try/Except Block causing ValueError

for my coding assignment I am to create a file that will read a csv file, offer different attributes to do analysis over (determined by the column values. I had this code working perfectly, but after I added my first try/except block I started getting the following error:
Traceback (most recent call last): File
"/Users/annerussell/Dropbox/Infotec 1040/module 8/csval.py", line 49,
in
row1=next(reader, 'end')[0:] ValueError: I/O operation on closed file.
Here is a link to a file you can test it with if desired. As you probably guessed this is a class assignment, and I am working on learning python for gradschool anyway so any suggestions are greatly appreciated.
import csv
print('Welcome to CSV Analytics!')
# Get file name and open the file
while True:
try:
file_name = input('Enter the name of the file you would like to process: ')
with open(file_name, "rt") as infile:
# Select the attribute to be analyzed
reader=csv.reader(infile)
headers=next(reader)[0:]
max=len(headers)
except FileNotFoundError:
print('The file you entered could not be found. Please' \
+ ' enter a valid file name, ending in .csv.')
continue
except IOError:
print('The file you selected could not be opened. Please ' \
+ 'enter a valid file name, ending in .csv.')
continue
except:
print('There was an error opening or reading your file. Please ' \
+ 'enter a valid file name, ending in .csv.')
continue
else:
print ('The attributes available to analyse are:')
for col in range(1, max):
print(col, headers[col])
while True:
try:
choice=int(input('Please select the number of the attribute you would like to analyze '))
except:
print('Please enter the numeric value for the selection you choose.')
continue
else:
# Build a dictionary with the requested data
dict1= {}
numrows=-1
row1=[]
largest_value=0
key_of_largest_value=0
while row1 != 'end':
row1=next(reader, 'end')[0:]
if row1 !='end':
numrows += 1
key=row1[0]
value=float(row1[choice])
dict1[key] = value
if value>largest_value:
largest_value=value
key_of_largest_value=key
# print('dictionary entry ( key, value)', key, value)
print('Largest ', headers[choice], ' value is ', key_of_largest_value, ' with ', largest_value)
In short: After with block ends, file is closed. You can't read from it, reader will fail.
Probably you didn't notice there is one-space indent for with, replace it with common indent so it will be more clear.
Seach for python context manager for more deep understanding.
Suggestion here is to factor out all logic from try else block to process_file function, and call it inside with statement.
with open(file_name, "rt") as infile:
# Select the attribute to be analyzed
reader=csv.reader(infile)
headers=next(reader)[0:]
max=len(headers)
process_file(reader, headers, max) # <-- like that
using with you need to move second condition to it block or
replace
with open(file_name, "rt") as infile:
with
isProcessing = True
while isProcessing:
....
infile = open(file_name, "rt")
...
#end of line
#print('Largest ',....
infile.close()
# end the loop
isProcessing = False

Error using langdetect in python: "No features in text"

Hey I have a csv with multilingual text. All I want is a column appended with a the language detected. So I coded as below,
from langdetect import detect
import csv
with open('C:\\Users\\dell\\Downloads\\stdlang.csv') as csvinput:
with open('C:\\Users\\dell\\Downloads\\stdlang.csv') as csvoutput:
writer = csv.writer(csvoutput, lineterminator='\n')
reader = csv.reader(csvinput)
all = []
row = next(reader)
row.append('Lang')
all.append(row)
for row in reader:
row.append(detect(row[0]))
all.append(row)
writer.writerows(all)
But I am getting the error as LangDetectException: No features in text
The traceback is as follows
runfile('C:/Users/dell/.spyder2-py3/temp.py', wdir='C:/Users/dell/.spyder2-py3')
Traceback (most recent call last):
File "<ipython-input-25-5f98f4f8be50>", line 1, in <module>
runfile('C:/Users/dell/.spyder2-py3/temp.py', wdir='C:/Users/dell/.spyder2-py3')
File "C:\Users\dell\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\dell\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/dell/.spyder2-py3/temp.py", line 21, in <module>
row.append(detect(row[0]))
File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector_factory.py", line 130, in detect
return detector.detect()
File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 136, in detect
probabilities = self.get_probabilities()
File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 143, in get_probabilities
self._detect_block()
File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 150, in _detect_block
raise LangDetectException(ErrorCode.CantDetectError, 'No features in text.')
LangDetectException: No features in text.
This is how my csv looks like
1)skunkiest smokiest yummiest strain pain killer and mood lifter
2)Relaxation, euphorique, surélevée, somnolence, concentré, picotement, une augmentation de l’appétit, soulager la douleur Giggly, physique, esprit sédation
3)Reduzierte Angst, Ruhe, gehobener Stimmung, zerebrale Energie, Körper Sedierung
4)Calmante, relajante muscular, Relajación Mental, disminución de náuseas
5)重いフルーティーな幸せ非常に強力な頭石のバースト
Please help me with this.
You can use something like this to detect which line in your file is throwing the error:
for row in reader:
try:
language = detect(row[0])
except:
language = "error"
print("This row throws and error:", row[0])
row.append(language)
all.append(row)
What you're going to see is that it probably fails at "重いフルーティーな幸せ非常に強力な頭石のバースト". My guess is that detect() isn't able to 'identify' any characters to analyze in that row, which is what the error implies.
Other things, like when the input is only a URL, also cause this error.
The error occurred when passing an object with no letters to detect. At least one letter should be there.
To reproduce, run any of below commands:
detect('.')
detect(' ')
detect('5')
detect('/')
So, you may apply some text pre-processing first to drop records in which row[0] value is an empty string,a null value, a white space, a number, a special character, or simply doesn't include any alphabets.
the problem is a null text or something like ' ' with no value;
check this in a condition and loop your reader in a list comprehension or
from langdetect import detect
textlang = [detect(elem) for elem in textlist if len(elem) > 50]
textlang = [detect(elem) if len(elem) > 50 else elem == 'no' for elem in textlist]
or with a loop
texl70 = df5['Titletext']
langdet = []
for i in range(len(df5)):
try:
lang=detect(texl70[i])
except:
lang='no'
print("This row throws error:", texl70[i])
langdet.append(lang)
The error occurrs when string has no letters. If you want to ignore that row and continue the process.
for i in df.index:
str = df.iloc[i][1]
try:
lang = detect(str)
except:
continue

reading last line of txt file in python and change it into variable to make calculation

td = 'date of transaction.txt'
tf = 'current balance.txt'
tr = 'transaction record.txt'
for line in open(tf):pass
for line2 in open(td):pass
for line3 in open(tr):pass
print line2,line,line3
"""
so call recall back last record
"""
rd=raw_input('\ndate of transaction: ')
print "Choose a type of transaction to proceed... \n\tA.Withdrawal \n\tB.Deposit \n\tC.Cancel & exit"
slc=raw_input('type of transaction: ')
i=1
while (i>0):
if slc=="A" or slc=="B" or slc=="C":
i=0
else:
i=i+1
slc=raw_input('invalid selection, please choose again...(A/B/C): ')
if slc=="A":
rr=input('\namount of transaction: ')
Line_len = 10 # or however long a line is, since in my example they all looked the same
SEEK_END = 2
file = open(tf, "r")
file.seek(-Line_len, SEEK_END)
a = int(str(file.read(Line_len)).split(" ")[0].strip())
rf=a-rr
f1=open(tf, 'a+')
f1.write('\n'+rf)
f1.close()
d1=open(td, 'a+')
d1.write('\n'+rd)
d1.close
r1=open(tr, 'a+')
r1.write('\n-'+rr)
r1.close
else:
print 'later'
above is my code, the function is to get data(last line) from txt file and read it, get new data and write it to the txt file again by creating new line.
my txt file(current balance.txt) should look like this:
2894.00
2694.00
but when i try to use the last line which is 2694.00 to do calculation(rf=a-rr), it failed returning this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
file.seek(-Line_len, SEEK_END)
IOError: [Errno 22] Invalid argument
else if i use this code:
for line in open(tf):
pass
a = line
rf=a-rr
it return this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
rf=a-rr
TypeError: unsupported operand type(s) for -: 'str' and 'int'
I seriously have no idea why...please help me...
To obtain last line of the file, you can simple do
with open('my_file.txt') as file:
last_line = file.readlines()[-1]
#last_line is a string value pointing to last line, to convert it into float, you can do
number = float(last_line.strip('\n').strip(' '))
The function input is giving you a string. Try doing:
rf=a-float(rr)

Python File reader convert invalid literal

I have been trying to make python code that reads a text file makes it a variable and sends it using i2c. Heres the code:
import serial
import smbus
import time
import sys
from time import sleep
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)
# This is the address we setup in the Arduino Program
address = 0x04
loopvar = 1
while loopvar == 1:
text_file = open("./PB_Vision/command.txt", "r")
print text_file.read()
visioncommando = text_file.read()
print visioncommando
def writeNumber(value):
bus.write_byte(address, value)
bus.write_byte_data(address, 0, value)
return -1
var = int(visioncommando)
writeNumber(var)
print ("RPI: Hi Arduino, I sent you")
print (var)
text_file.close()
when I run this, I get this as output;
110
Traceback (most recent call last):
File "testai.py", line 29, in <module>
var = int(visioncommando)
ValueError: invalid literal for int() with base 10: ''
any suggestion, fixes?
anyways thanks in advance.
Thanks your suggestions worked, one step closer to an working project.
if your command.txt file contains:
110
Then the following code is wrong:
while loopvar == 1:
text_file = open("./PB_Vision/command.txt", "r")
print text_file.read()
visioncommando = text_file.read()
print visioncommando
Your 2nd text_file.read() call will result in "" (en empty string) because you've already exhausted the contents of the open file object.
You could do something liket his instead:
while loopvar == 1:
text_file = open("./PB_Vision/command.txt", "r")
visioncommando = text_file.read()
print visioncommando
Remove this:
print text_file.read()
and it will work.
Iterators can only be used once...
You read the file in the wrong way, python convert 110 to int but it find an empty string '' after that. So I would suggest to add a condition before int() command and str.isdigit() is a good candidate.

Categories

Resources