At standard input, I am providing the following file:
#123 595739778 "neutral" Won the match #getin
#164 595730008 "neutral" Good girl
data#2 looks like this:
labels 1 0 -1
-1 0.272653 0.139626 0.587721
1 0.0977782 0.0748234 0.827398
I want to see if its -1 in the data#2 file then replace with negative, 1 then positive and 0 then neutral
Following are my issues:
start in data#2 file on 2nd line
I am facing trouble with replace. I want to replace like below but its showing a error tht it expects 1 more argument but I already have 2 arguments there.
If I do, something like below (notice the print statement):
if binary == "-1":
senti = str.replace(senti.strip('"'),"negative")
elif binary == "1":
senti = str.replace(senti.strip('"'),"positive")
elif binary == "0":
senti = str.replace(senti.strip('"'),"neutral")
print id, "\t", num, "\t", senti, "\t", sent
but if I do(notice print) then it does not go in the 'if conditions':
if binary == "-1":
senti = str.replace(senti.strip('"'),"negative")
elif binary == "1":
senti = str.replace(senti.strip('"'),"positive")
elif binary == "0":
senti = str.replace(senti.strip('"'),"neutral")
print id, "\t", num, "\t", senti, "\t", sent
How do I print then.
output that I got:
#123 595739778 "neutral" Won the match #getin
#164 595730008 "neutral" Good girl
output expected (replace just replaces the negative, positive & neutral as per data# file:
#123 595739778 negative Won the match #getin
#164 595730008 positive Good girl
Error:
Traceback (most recent call last):
File "./combine.py", line 17, in <module>
senti = str.replace(senti.strip('"'),"negative")
TypeError: replace() takes at least 2 arguments (1 given)
Here is my code:
for line in sys.stdin:
(id,num,senti,sent) = re.split("\t+",line.strip())
tweet = re.split("\s+", sent.strip().lower())
f = open("data#2.txt","r")
for line1 in f:
(binary,rest,rest1,test2) = re.split("\s", line1.strip())
if binary == "-1":
senti = str.replace(senti.strip('"'),"negative")
elif binary == "1":
senti = str.replace(senti.strip('"'),"positive")
elif binary == "0":
senti = str.replace(senti.strip('"'),"neutral")
print id, "\t", num, "\t", senti, "\t", sent
You are in fact missing an argument for replace; since it is a method of the string itself, you could do either:
In [72]: str.replace('one','o','1')
Out[72]: '1ne'
or
In [73]: 'one'.replace('o','1')
Out[73]: '1ne'
In your code, you likely want, e.g.
if binary == "-1":
senti = senti.strip('"').replace("-1","negative")
To skip the first line of your data#2 file, one option is
f = open("data#2.txt","r")
for line1 in f.readlines()[1:]: # skip the first line
#rest of your code here
Edit: After a chat conversation, what I think you want is more like the following:
f = open("data#2.txt","r")
datalines = f.readlines()[1:]
count = 0
for line in sys.stdin:
if count == len(datalines): break # kill the loop if we've reached the end
(tweetid,num,senti,tweets) = re.split("\t+",line.strip())
tweet = re.split("\s+", tweets.strip().lower())
# grab the right index from our list
(binary,rest,rest1,test2) = re.split("\s", datalines[count].strip())
if binary == "-1":
sentiment = "negative"
elif binary == "1":
sentiment = "positive"
elif binary == "0":
sentiment = "neutral"
print tweetid, "\t", num, "\t", sentiment, "\t", tweets
count += 1 # add to our counter
Related
Reading in the data text file,
Running loops to check criteria for valid and invalid student numbers.
Then trying to write 2 text files, one for the invalid student numbers and one for the valid student numbers
This far everything works accept no text files written and no data written to text files at the end
Here is the Script so far
inputList = []
outputList = []
def FileOpen(myList):
#try:
count=0
INFILE=open("data.txt", "r")
for line in INFILE:
myList.append(line.rstrip())
count+=1
INFILE.close()
return count
#except:
# print("File could not be found")
# exit()
FileOpen(inputList)
print(FileOpen(inputList),"number of lines read from file")
def AnalyseStudents(rawStudents,totalStudents):
for entry in rawStudents:
amountOfDigits = len(entry)
testOfDigits = entry.isdigit()
def inValid1(val1,val2):
answ = val1 != 8 and val2 != True
return answ
inValidResult1=(inValid1(amountOfDigits,testOfDigits))
def Valid1(val1,val2):
answ = val1 == 8 and val2 == True
return answ
validResult1=(Valid1(amountOfDigits,testOfDigits))
if inValidResult1:
print(entry, " is an INVALID student number")
elif validResult1:
a=entry[0]
b=entry[1]
c=entry[2]
d=entry[3]
e=entry[4]
f=entry[5]
g=entry[6]
h=entry[7]
sum = float((a*8)+(b*7)+(c*6)+(d*5)+(e*4)+(f*3)+(g*2)+(h*1))
result = sum%11
def inValid2 (val):
answ = val != 0
return answ
inValidResult2=(inValid2(result))
def Valid2 (val):
answ = val == 0
return answ
validResult2=(Valid2(result))
if validResult2:
print(entry, " is an VALID student number")
elif inValidResult2:
print(entry, " is an INVALID student number")
totalStudents
AnalyseStudents(inputList,outputList)
def Write(outList):
if outList == (" is an VALID student number"):
OUTFILE1=open("ValidNumbers.txt","w")
for validResult in outList:
OUTFILE1.write(validResult+"\n")
OUTFILE1.close()
elif outList == (" is an INVALID student number"):
OUTFILE2=open("InvalidNumbers.txt","w")
for inValidResults in outList:
OUTFILE2.write(inValidResults+"\n")
OUTFILE2.close()
Write(outputList)
print("See output files for more details")
Your equality statements are wrong
if outList == (" is an VALID student number"):
...
elif outList == (" is an INVALID student number"):
The outList is a list but you are comparing it to strings. Put a debug breakpoint at those if statements and decide what you want.
I'm still working my way through this, but for the first function, you can use list comprehension and a couple of other Python functions. Commentary is included.
inputList = []
outputList = []
# Change parameter to the name of the file you want to open.
# Here, we can just make the parameter optional and pass your
# text file name by default.
def FileOpen(filename="data.txt"):
# Using with keeps the open file in context until it's finished
# it will then close automatically.
with open(filename, "r") as INFILE:
# Reach all the data at once and then try to decode it.
data = INFILE.read()
try:
data = data.decode("utf-8")
# If data can't be decoded, then it doesn't have that attribute.
# Catch that exception
except AttributeError:
pass
# Python 3 has a string method called .splitlines() that makes
# splitting lines a little simpler.
myList = [line for line in data.splitlines()]
# We'll return two values: The length (or count) of the list
# and the actual list.
return len(myList), myList
# We set up two variables for intake of our count and list results.
count, myList = FileOpen()
print("{} number of lines read from file".format(count))
EDIT: I don't know what the last part of your code is aiming to do. Maybe compare student count to valid entries? Here's the middle part. I moved the function outside and made it evaluate either valid or invalid in one fell swoop.
def is_valid(entry_value):
"""Validate current entry."""
if len(entry_value) != 8 and entry_value.isnumeric():
return False
return True
def AnalyseStudents(rawStudents, totalStudents):
for entry in rawStudents:
# Our new function will return True if the entry is valid
# So we can rework our function to run through the True condition
# first.
if is_valid(entry):
entry_sum = entry[0] * 8
entry_sum += entry[1] * 7
entry_sum += entry[2] * 6
entry_sum += entry[3] * 5
entry_sum += entry[4] * 4
entry_sum += entry[5] * 3
entry_sum += entry[6] * 2
entry_sum += entry[7] * 1
if entry_sum % 11 == 0:
print(entry, " is an VALID student number")
else:
print(entry, " is an INVALID student number")
else:
print(entry, " is an INVALID student number")
AnalyseStudents(inputList, outputList)
I have a program that splits a text into sentences using the following rule:
Sentence boundaries occur at ".", "?" and "!" except:
A) Periods followed by digit with no intervening whitespaces. B) Periods followed by whitespaces followed by lower case letters. C) Periods with no followed whitespaces. D) Periods preceded with titles.
My written code is as given below:
file_name = raw_input("Enter the name of the text file: ")
txt_file = open('%s.txt' % file_name, 'r+')
text = txt_file.readline()
print; print "Original text is: "; print
print text
new_wrd = []
new_line = []
new_txt = []
while len(text.strip()) != 0:
for index, char in enumerate(text):
print char
if char == "." or char == "?" or char == "!":
if text[index+1] == " ":
if ("".join(new_wrd) == "Mrs" or "".join(new_wrd) == "Mr" or "".join(new_wrd) == "Ms"
or "".join(new_wrd) == "Dr" or "".join(new_wrd) == "Jr"):
new_wrd.append(char)
else:
if text[index+2].isupper():
new_line.append("".join(new_wrd))
new_line.append(char)
new_txt.append("".join(new_line))
new_line = []
new_wrd = []
else:
new_line.append("".join(new_wrd))
new_line.append(char + " ")
new_wrd = []
else:
new_wrd.append(char)
elif char == " ":
if ("".join(new_wrd) == "Mrs." or "".join(new_wrd) == "Mr." or "".join(new_wrd) == "Ms."
or "".join(new_wrd) == "Dr." or "".join(new_wrd) == "Jr.") or new_wrd != []:
new_line.append("".join(new_wrd))
new_line.append(" ")
new_wrd = []
else:
new_wrd.append(char)
text = txt_file.readline()
for txt in new_txt:
print txt
txt_file.write(txt)
For the given example:
Mr. XYZ is a good boy. He has just pass his B.Tech degree from ABC, Lmnop... At least, he has passed the degree.
The output should show:
Mr. XYZ is a good boy.
He has just pass his B.Tech degree from ABC, Lmnop...
At least, he has passed the degree.
But instead, it shows:
Mr. XYZ is a good boy.
He has just pass his B.Tech degree from ABC, Lmnop...
What are the corrections that could be made for proper output?
Also, the the code:
txt_file.write(txt)
not working. Why?
I cant figure out why line 104 keeps returning invalid syntax, can someone please point me in the right direction? Does it have something to do with the way i used elif? Sorry if this is a newbie question!
Line 104 is the else statement in the for item in workingDict.keys() loops inside printGrammar
import sys
import string
from collections import defaultdict
#default length of 3
stringLength = 3
#get last argument of command line(file)
if len(sys.argv) == 1:
#get a length from user
try:
stringLength = int(input('Length? '))
filename = input('Filename: ')
except ValueError:
print("Not a number")
elif len(sys.argv) == 2:
#get a length from user
try:
stringLength = int(input('Length? '))
filename = sys.argv[1]
except ValueError:
print("Not a number")
elif len(sys.argv) == 3:
filename = sys.argv[2]
stringLength = sys.argv[1].split('l')[1]
else:
print("Invalid input!")
#get start symbol
with open(filename, "r") as grammar:
#read file
lines = grammar.readlines()
start = lines[0].split('=')[0]
start = start.replace(" ", "")
#checks
#print(stringLength)
#print(filename)
#print(start)
def str2dict(filename):
result = defaultdict(list)
with open(filename, "r") as grammar:
#read file
lines = grammar.readlines()
count = 0
#loop through
for line in lines:
#append info
line = line.rstrip()
result[line[0]].append(line.split('=')[1])
return result
workingDict = str2dict("Binary.txt")
print(workingDict)
def printGrammar(result):
sentenceList = []
workList = []
workList.append(start)
i = 0
firstNT = ""
#While the worklist is not empty:
while(len(workList) != 0):
#Get and delete one potential sentence s from the worklist.
symbol = workList.pop()
#If the | s | > N, continue.
if len(str(symbol).replace(" ", "")) > int(stringLength):
continue
else:
if str(symbol) in workingDict.keys():
#append the right derivation
for item in workingDict.get(symbol):
workList.append(list(item.replace(" ", "")))
#workList.append(str(workingDict.get(symbol)))
#add derivation
print(workingDict.keys())
#If s has no nonterminals, print s and continue.
for item in workingDict.keys():
print("test")
print(''.join(item))
if len(item) != 1:
continue
#if the element is in dictionary, dont print
elif ''.join(item) in workingDict.keys():
continue
print(symbol)
#if element is not in dictionary, print
else:
print("THIS IS A TERMINAL!!")
print(item)
#Choose the leftmost nonterminal NT.
print(workList)
#For all productions NT -> rhs:
#Replace NT in s with rhs; call it tmp.
#Store tmp on worklist.
return workList
print (printGrammar(workingDict))
You need to indent the line
print(symbol)
to the same level as continue.
while True:
try:
file = input("Enter a filename: ")
fi = open(file, "r")
infile = fi.read()
grid = [list (i) for i in infile.split()] #Puts the sudoku puzzle into a list in order to check that the total number is valid
check = len(grid)
print("The total number in this puzzle is:",check) #Counts the amount of numbers in the sudoku puzzle
break
except FileNotFoundError:
print ("The inputted file does not exist")
def check(infile):
count = 0
for j in range (0,9):
for n in range(0,9):
if infile[j].count(infile[j][n]) <= 1:
count = count + 0
else:
count = count + 1
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
leg = 0
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) <= 1:
leg = leg + 0
else:
leg = leg + 1
angel = []
for t in range(3):
ang = infile[t]
for u in range(3):
angel.append(ang[u])
foot = 0
for be in range(9):
if angel.count(angel[be]) <= 1:
foot = foot + 0
else:
foot = foot + 1
if count + leg + foot == 0:
print("Valid")
else:
print ("Invalid")
def inputs():
x = raw_input()
ls = []
while x != '':
x1 =x.split(' ')
ls.append(x1)
if len(infile) >=9:
print (check(infile))
infile = []
x = raw_input()
inputs()
actual error:
Traceback (most recent call last):
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <module>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <listcomp>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <listcomp>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
IndexError: string index out of range
Why does it give an output to say that my string index is out of range, is there another way to create a sudoku 9x9 checker to check if there are any reoccurring numbers. I need to make sure that there are 9 numbers in each column and that they are between the numbers 1 and 9
first, a few comments:
never do:
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
but do:
cols = [[row[i] for row in infile] for i in range(0,9)]
never call a variable the same name as a function you've defined in your code check and check()
don't write code at the module level, but embed everything in functions, and call the entry point function at the end of the file after the if __name__ == "__main__" condition (so in case you want to import your module in another module, you don't execute module level code).
don't open files without closing them, instead use the context manager: with open('myfile', 'r') as f: ...
your code features an useless use of while... or at least a wrong use (do you really mean to loop forever on an exception?) use command line arguments instead, that will make the shell help your user choose a file that does actually exists.
now I've made all that clear, here's about your actual question:
infile is a file object (if I can read correctly your mis-indented python code), thus every line - called row here - of infile is just a string.
So if you have an empty line or a line that is less than 9 columns, you're likely to get row[i] out of boundaries.
here's a take at refactoring your code, though I've left a number of wrong design over:
def check(infile):
count = 0
for j in range (0,9):
for n in range(0,9):
if infile[j].count(infile[j][n]) <= 1:
count = count + 0
else:
count = count + 1
def inputs():
x = raw_input()
ls = []
while x != '':
x1 =x.split(' ')
ls.append(x1)
if len(infile) >=9:
print (check(infile))
infile = []
x = raw_input()
def check_grid():
cols = [[row[i] for row in infile] for i in range(0,9)]
leg = 0
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) <= 1:
leg = leg + 0
else:
leg = leg + 1
angel = []
for t in range(3):
ang = infile[t]
for u in range(3):
angel.append(ang[u])
foot = 0
for be in range(9):
if angel.count(angel[be]) <= 1:
foot = foot + 0
else:
foot = foot + 1
if count + leg + foot == 0:
print("Valid")
else:
print ("Invalid")
inputs()
def sudoku_checker():
try:
file = input("Enter a filename: ")
fi = open(file, "r")
infile = fi.read()
grid = [list (i) for i in infile.split()] #Puts the sudoku puzzle into a list in order to check that the total number is valid
# Counts the amount of numbers in the sudoku puzzle
print("The total number in this puzzle is:",len(grid))
check_grid()
except FileNotFoundError:
print ("The inputted file does not exist")
if __name__ == "__main__":
sudoku_checker()
elif used_prefix and cmd == "xp":
if self.getAccess(user) >= 1:
f = open("users/" + user.name.lower() + ".txt", 'r')
word = f.readline().split("X Points = ")
if word == "0":
room.message("You have no X Points")
else:
room.message("You Have " + word + " X Points")
f.close()
else:
room.message("You are not whitelisted " + user.name.capitalize())
When I try to use XP it shows Can't convert 'list' object to str implicitly as the error in the console. I'm using python 3.3.
You might need
word = f.readline().split("X Points = ")[1].strip()
as you are splitting, it will return the list of items split as a list. You need to take the element corresponding to the actual value
Example
data = "X Points = 10"
print data.split("X Points = ")
Output
['', '10']
So, we need to get the second element. Thats why we use [1]
The main issue is that split returns a list, not a string.
if self.getAccess(user) >= 1:
with open("users/{}.txt".format(user.name.lower()), 'r') as f:
word = f.readline().split("X Points = ")[1]
if word == "0":
room.message("You have no X Points")
else:
room.message("You Have {} X Points".format(word))
else:
room.message("You are not whitelisted {}".format(user.name.capitalize()))