Getting an error because of the " \ " characters - python

I'm getting:
"unexpected character after line continuation character"
How should I write the line = line.strip("\xef\xbb\n\xbf")line without getting that error.
dataFile = open("data.txt","r")
updateFile = open("update","r")
newFile = open("newdata","w")
dataMatrix = []
updateMatrix = []
cardList = []
for line in dataFile:
line = line.strip("\xef\xbb\n\xbf")
tmp = line.split(" ")
cardNum = tmp[0]
cardName = " ".join(tmp[1:-2])
cardDate = tmp[-2]
cardSum = tmp[-1]
dataMatrix.append([cardNum,cardName,cardDate,cardSum])
cardList.append(cardNum)
i = 0
updateDate = ""
for line in updateFile:
line = line.strip("\xef\xbb\n\xbf")
if i==0 : updateDate = line; i=1; continue;
tmp = line.split(" ")
upNum = tmp[0]
upName = " ".join(tmp[1:-1])
upSum = tmp[-1]
updateMatrix.append([upNum,upName,upSum])
for row in updateMatrix:
if row[0] in cardList:
index = cardList.index(row[0])
plus = row[2]
if plus[0] == "+":
plus = int(plus[1:])
else:
plus = -int(plus[1:])
curSum = int(dataMatrix[index][3])
newSum = curSum+plus
dataMatrix[index][3] = newSum
dataMatrix[index][2] = updateDate
# dataMatrix[index][]
else:
dataMatrix.append([row[0],row[1],updateDate,row[2][1:]])
dataMatrix.sort(key=lambda row: row[0])
for row in dataMatrix:
print row
newFile.write(" ".join(str(a) for a in row) + "\n")

Related

How to Make Automatically? Panning Matplotlib Function

So I have been trying to make a function in Python that makes my graph automatically pan rather than using the buttons on the Matplotlib graph.
(the MultiScatter function)
The way the code works is that it looks at a file and takes the numbers inside and turns them into arrays with x being each "beat" and y being the different types of numbers. And I want to take those arrays and turn those into a graph that automatically pans from the first beat to the last one.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
import numpy as np
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import os
import glob
import time
directions = []
nameDirections = []
beats = 0
root = Tk()
nameDirections = ["left", "down", "up", "right"]
directions = []
#NameDirections are the type of arrows that are being measured.
#Directions contains the numbers that measure the amount of times each direction is in the text file for a Dance Dance Revolution game code
e = Entry(root, width=50)
e.pack()
root.title("Insert GUI Title Here")
root.geometry('600x400')
root.resizable(False, False)
#Size of GUI
def openFolder():
folPath = filedialog.askdirectory()
return folPath
def openSmFile():
folPath = filedialog.askopenfilename()
return folPath
#Opens an SM file (Stepmania code file)
def checkDirections():
folPath = openFolder()
for fpath in glob.iglob(f'{folPath}/*'):
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
left = 0
down = 0
up = 0
right = 0
beats = 0
for line in lines:
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or len(line) != 5):
for alpha in line:
if i == 0 and alpha != "0":
left += 1
if i == 1 and alpha != "0":
down += 1
if i == 2 and alpha != "0":
up += 1
if i == 3 and alpha != "0":
right += 1
i += 1
beats += 1
print("The file name is " + str(fpath))
print("There are " + str(left) + " lefts in this song.")
print("There are " + str(down) + " downs in this song.")
print("There are " + str(up) + " ups in this song.")
print("There are " + str(right) + " rights in this song.")
print("There are " + str(beats) + " beats.")
#Prints number of each type of arrow. This was taken from code I wrote earlier and I just changed it up for the near-identical function below that returns instead
#Because i was too lazy to make a good solution
def graph(thing):
fpath = openSmFile()
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
left = 0
down = 0
up = 0
right = 0
for line in lines:
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == ""):
for alpha in line:
if i == 0 and alpha != "0":
left += 1
if i == 1 and alpha != "0":
down += 1
if i == 2 and alpha != "0":
up += 1
if i == 3 and alpha != "0":
right += 1
i += 1
directions = [left, down, up, right]
plt.title(str(os.path.basename(fpath)))
if (thing == "bar"):
plt.bar(nameDirections, directions)
if (thing == "pie"):
plt.pie(directions, labels=nameDirections, autopct='%1.1f%%',shadow=True, startangle=90)
if (thing == "scatter"):
plt.scatter(directions, nameDirections)
else:
print("This file is not valid.")
def ScatterTime(text):
if (text == ""):
fpath = openSmFile()
else:
fpath = text
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
arrowDirections = []
xs = []
ys = []
x = 0
counter = 0
for line in lines:
if counter > 250:
break
y = -1
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or ":" in line or len(line) != 5):
for alpha in line:
if i == 0 and alpha != "0":
xs.append(x)
ys.append(0)
arrowDirections.append("left")
if i == 1 and alpha != "0":
xs.append(x)
ys.append(1)
arrowDirections.append("down")
if i == 2 and alpha != "0":
xs.append(x)
ys.append(2)
arrowDirections.append("up")
if i == 3 and alpha != "0":
xs.append(x)
ys.append(3)
arrowDirections.append("right")
i += 1
x = x + 1
counter = counter + 1
#print(len(ys))
plt.title(str(os.path.basename(fpath)))
# fullrange = list(range(1, beat))
#u, ind = np.unique(arrowDirections, return_inverse=True)
#plt.xticks(range(len(u)), u)
#plt.scatter(ind, fullrange, s=beats * 10, marker = ".",)
plt.scatter(xs,ys)
else:
print("This file is not valid.")
#Creates a scatterplot
def returnCounter(thing):
fpath = thing
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
counter = 0
for line in lines:
if counter > 250:
break
counter = counter + 1
return counter
else:
print("This file is not valid.")
def returnSmFile():
return openSmFile()
def returnXs(text):
if (text == ""):
fpath = openSmFile()
else:
fpath = text
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
arrowDirections = []
xs = []
ys = []
x = 0
counter = 0
for line in lines:
if counter > 250:
break
y = -1
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or ":" in line or len(line) != 5):
for alpha in line:
if i == 0 and alpha != "0":
xs.append(x)
ys.append(0)
arrowDirections.append("left")
if i == 1 and alpha != "0":
xs.append(x)
ys.append(1)
arrowDirections.append("down")
if i == 2 and alpha != "0":
xs.append(x)
ys.append(2)
arrowDirections.append("up")
if i == 3 and alpha != "0":
xs.append(x)
ys.append(3)
arrowDirections.append("right")
i += 1
x = x + 1
plt.title(str(os.path.basename(fpath)))
return(xs)
else:
print("This file is not valid.")
def returnYs(text):
if (text == ""):
fpath = openSmFile()
else:
fpath = text
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
arrowDirections = []
xs = []
ys = []
x = 0
counter = 0
for line in lines:
if counter > 250:
break
y = -1
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or ":" in line or len(line) != 5):
for alpha in line:
if i == 0 and alpha != "0":
xs.append(x)
ys.append(0)
arrowDirections.append("left")
if i == 1 and alpha != "0":
xs.append(x)
ys.append(1)
arrowDirections.append("down")
if i == 2 and alpha != "0":
xs.append(x)
ys.append(2)
arrowDirections.append("up")
if i == 3 and alpha != "0":
xs.append(x)
ys.append(3)
arrowDirections.append("right")
i += 1
x = x + 1
plt.title(str(os.path.basename(fpath)))
return(ys)
else:
print("This file is not valid.")
#Creates a scatterplot
def testFunction():
fpath = openSmFile()
if (fpath.endswith('.sm')):
file = open(fpath, "r")
lines = []
lines = file.readlines()
for line in lines:
i = 0
if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or ":" in line or len(line) != 5):
print(line)
print(len(line))
else:
print("This file is not valid.")
#Not relevant. Tests arrays in function
def bar():
graph("bar")
plt.show()
#Creates a bar graph
def ILovePie():
graph("pie")
plt.show()
def Scatter():
ScatterTime("")
plt.show()
def multiScatter():
sm = returnSmFile()
Ys = returnYs(sm)
Xs = returnXs(sm)
counter = returnCounter(sm) / 10
plt.figure(num=None, figsize = [counter, 3])
plt.scatter(Xs, Ys)
while (counter != returnCounter(sm)):
plt.show()
counter += returnCounter(sm)
plt.figure(num=None, figsize = [counter, 3])
plt.scatter(Xs, Ys)
counter += returnCounter(sm) / 10
time.sleep(0.2)
barGraph = Button(root, text="Click to show a bar graph", command=bar)
pieGraph = Button(root, text = "Click to show a pie graph", command = ILovePie)
runThrough = Button(root, text="Click to print number of each arrow", command=checkDirections)
scatterGraph = Button(root, text = "Click to show a scatterplot", command = Scatter)
testButton = Button(root, text = "Test", command = testFunction)
multiButton = Button(root, text = "Show each section of the scatterplot", command = multiScatter)
barGraph.pack()
runThrough.pack()
pieGraph.pack()
scatterGraph.pack()
testButton.pack()
multiButton.pack()
root.mainloop()
#Creates the buttons to show the graphs and the run text command```

How Do I Replace Lines in a for Loop

The File I'm editing
45940736:1330:0.266667:1602990684:10023084.555000
48545806:16000000:0.000000:9999999999:0.000000
1191125008:1185:37.408333:1602991293:10282893.310000
116776982:1811:0.637500:1602990076:10022476.137000
My Code
f = open("values", "r+")
lines = f.readlines()
for i in lines:
if str(IDS) in i:
spl = i.split(":")
asset_id = spl[0]
value = spl[1]
volume = spl[2]
updated = spl[3]
item_age = spl[4]
new_line = asset_id + ":" + "this is a test" + ":"+ volume + ":" + updated + ":" + item_age
old_line = i
print(new_line)
How would I replace the line while in the for loop instead of doing
lines[0] = new_line
ff = open('values', 'w')
ff.writelines(lines)
I can't do that because some files will have 2k values and I need to change all of them.
Use enumerate() so you get the index in the list, so you can replace the element.
for index, i in enumerate(lines):
if str(IDS) in i:
spl = i.split(":")
asset_id = spl[0]
value = spl[1]
volume = spl[2]
updated = spl[3]
item_age = spl[4]
new_line = asset_id + ":" + "this is a test" + ":"+ volume + ":" + updated + ":" + item_age
old_line = i
lines[index] = new_line

IndexError: string index out of range <making encrypt, decrypt program>

import random
#get filename
name = input('Enter filename: ')
#load file
try:
input_file = open(name, 'r')
except IOError:
print('File does not exist. Program will terminate.')
#make key value
line = input_file.readline()
key = []
key_mix = []
for i in range(len(line)):
if line[i] not in key:
key.append(line[i])
for i in range(len(line)):
if line[i] not in key_mix:
key_mix.append(line[i])
random.shuffle(key_mix)
#encryption
if name.split('.')[1] == 'txt':
try:
key_file = open(name.split('.')[0] + '.key', 'w')
enc_file = open(name.split('.')[0] + '.enc', 'w')
except IOError:
print('File does not exist. Program will terminate.')
key_write = ['']
for g in range(len(key)):
key_write[0] += key_mix[g]
for i in range(len(key)):
keys = str(key[i]) + ',' + str(key_mix[i])
key_file.write(keys+'\n')
couple = {}
for k in range(len(key)):
couple[key[k]] = key_mix[k]
enc = ['']
for t in range(len(line)):
enc[0] += couple.get(line[t])
enc_file.write(enc[0])
input_file.close()
key_file.close()
enc_file.close()
#decryption
elif name.split('.')[1] == 'enc':
try:
key_file = open(name.split('.')[0] + '.key', 'r')
dec_file = open(name.split('.')[0] + '.txt', 'w')
except IOError:
print('File does not exist. Program will terminate.')
line = input_file.readline()
dec = ['']
sol = {}
while True:
sen = key_file.readline()
if not sen: break
sol.update({sen[2]:sen[0]})*Problem Here*
for m in range(len(line)):
dec[0] += sol.get(line[m])
dec_file.write(dec[0])
input_file.close()
key_file.close()
dec_file.close()
It makes error:
IndexError: string index out of range
and when I check my .key file, it comes like
t,o
h,l
e,s
r,h
i,t
s,r
,n
n,v
o,u
u,e
f,i
l,f
v,
but when I print readline, it comes like
t,o
(blank)
e,s
(blank)
i,t
(blank)
,n
(blank)
o,u
(blank)
f,i
(blank)
v,
(blank)
How can I fix it?

Python 'Tuple' object has no attribute 'has_key'

I'm running a piece of freely available python code used to detect CNVs in single cell sequencing data:
#!/usr/bin/env python
import sys
def main():
infilename = sys.argv[1]
outfilename = sys.argv[2]
statfilename = sys.argv[3]
chrominfo = ("/path/hg19.chrom.sizes.txt", 0)
bins = ("/path/hg19.bin.boundaries.50k.bowtie.k50.sorted.txt", 0)
INFILE = open(infilename, "r")
OUTFILE = open(outfilename, "w")
STATFILE = open(statfilename, "w")
binCounts = []
for i in range(len(bins)):
binCounts.append(0)
print len(binCounts)
print len(bins)
counter = 0
totalReads = 0
prevChrompos = ""
for x in INFILE:
arow = x.rstrip().split("\t")
thisChrom = arow[2]
thisChrompos = arow[3]
if thisChrom.find("_") > -1:
#print thisChrom
continue
if thisChrom == "chrM":
#print thisChrom
continue
if thisChrom == "":
continue
if chrominfo.has_key(thisChrom):
pass
else:
continue
totalReads += 1
thisChrominfo = chrominfo[thisChrom]
thisAbspos = long(thisChrompos) + long(thisChrominfo[2])
counter += 1
indexUp = len(bins) - 1
indexDown = 0
indexMid = int((indexUp - indexDown) / 2.0)
while True:
if thisAbspos >= long(bins[indexMid][2]):
indexDown = indexMid + 0
indexMid = int((indexUp - indexDown) / 2.0) + indexMid
else:
indexUp = indexMid + 0
indexMid = int((indexUp - indexDown) / 2.0) + indexDown
if indexUp - indexDown < 2:
break
binCounts[indexDown] += 1
prevChrompos = thisChrompos
for i in range(len(binCounts)):
thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
OUTFILE.write("\t".join(bins[i][0:3]))
OUTFILE.write("\t")
OUTFILE.write(str(binCounts[i]))
OUTFILE.write("\t")
OUTFILE.write(str(thisRatio))
OUTFILE.write("\n")
binCounts.sort()
STATFILE.write("TotalReads\tMedianBinCount\n")
STATFILE.write(str(totalReads))
STATFILE.write("\t")
STATFILE.write(str(binCounts[len(bins)/2]))
STATFILE.write("\n")
INFILE.close()
OUTFILE.close()
STATFILE.close()
def fileToDictionary(inputFile, indexColumn):
input = open(inputFile, "r")
rd = dict()
# input.readline()
for x in input:
arow = x.rstrip().split("\t")
id = arow[indexColumn]
if rd.has_key(id):
#rd[id].append(arow)
print "duplicate knowngene id = " + id
print "arow = " + str(arow)
print "rd[id] = " + str(rd[id])
else:
rd[id] = arow
input.close()
return(rd)
def fileToArray(inputFile, skipFirst):
input = open(inputFile, "r")
ra = []
for i in range(skipFirst):
input.readline()
for x in input:
arow = x.rstrip().split("\t")
ra.append(arow)
input.close()
return(ra)
if __name__ == "__main__":
main()
I'm getting an error on line 40:
Traceback (most recent call last):
File "/path/varbin.50k.sam.py", line 129, in <module>
main()
File "/path/varbin.50k.sam.py", line 40, in main
**if chrominfo.has_key(thisChrom):
AttributeError: 'tuple' object has no attribute 'has_key'**
I don't work regularly in Python, can someone offer a suggestion?
Where do I begin?
Your code is expecting a dictionary and getting a tuple. I think you've missed a step: You need to change
chrominfo = ("/path/hg19.chrom.sizes.txt", 0)
To
chrominfo = fileToDictionary("/path/hg19.chrom.sizes.txt", 0)
Note also that if dict.has_key(key) has been deprecated in favour of if key in dict.keys()

Python read data in as binary

I am wanting to read in the logData as binary and then parse the binary output in the second for loop as it is for a string but for binary. Is this possible?
logData = open(sys.argv[1]).readlines()
processedSources = sys.stdin.readlines()
stringDictionary = {}
for line in processedSources:
# Match data looking for MODULE_ID, LOG_LINE, ARG_COUNT, FILE_NAME, DATA_STRING
match = re.search("(\d+),\s+(\d+),\s+(\d+),\s+(.*),\s+(\".*\")", line)
if match:
moduleId = int(match.group(1))
logLine = int(match.group(2))
argCount = int(match.group(3))
fileName = match.group(4)
outputString = match.group(5)
stringDictionary[(moduleId, logLine)] = [ moduleId, logLine, argCount, fileName, outputString ]
else:
print "Failed string dictionary on: " + line
for line in logData:
# Match data looking for MODULE_ID, LOG_LINE, ARG_COUNT, ARGUMENTS
matchLogData = re.split("\s+", line)
if matchLogData:
moduleId = int(matchLogData[0], 16)
logLine = int(matchLogData[1], 16)
argCount = int(matchLogData[2], 16)
if stringDictionary[(moduleId, logLine)]:
processedData = stringDictionary[(moduleId, logLine)]
if argCount != processedData[2]:
print "Argument count mismatch on : " + line
print " expected %d found %d" % (argCount, processedData[2])
else:
index = 0
logString = "%02x:%4d:%s:" + processedData[4]
logData = (processedData[0], processedData[1], processedData[3])
while index < argCount:
logData = logData + (int(matchLogData[index+3], 16),)
index = index + 1
print logString % logData
else:
print "ModuleId:%d Line:%d, not found in source dictionary" % (moduleId, logLine)
print " Line data: " + line
else:
print "Expected log input data mismatch MODULE_ID LOG_LINE ARG_COUNT ARGS"
print "Line: " + line

Categories