python files - who to open corectly - python

We have file with some math problems like: 46 + 19 (only + or - and it built up this way: number, space, sign, space, number) and we need to transform it into a new file and solve them (46 + 19 = 65). We don't know how many exercises there will be or the number of digits in every number. Here is my code:
enter code here
input_file = open(r'C:\try\bla.txt', 'r')
nums = input_file.read()
y = 0
dig1 = ''
dig2 = ''
sign = ''
x1 = nums.find(' ')
x2 = x1 + 1
def one(dig1, dig2, y):
for i in xrange(x1):
dig1 += nums[y]
y += 1
for m in xrange(abs(-x2)):
dig2 += nums[y + 1]
y += 1
sign = nums[x2]
if sign == '+':
sum = int(dig1) + int(dig2)
if sign == '-':
sum = int(dig1) - int(dig2)
print dig1, dig2, '=', sum
for a in xrange(0):
one(dig1, dig2, y)
one(dig1, dig2, y)
print 'f', nums[21]
#print dig1, dig2, '=', sum

Maybe you are want to get this(python3):
test.txt:
10 + 15
22 - 71
33 + 64
code:
import operator
op = {'+': operator.add, '-': operator.sub}
with open('test.txt', 'r') as f:
lines = f.readlines()
for i in lines:
args = i.split()
val = op[args[1]](int(args[0]), int(args[-1]))
r = f'{i.strip()} = {val}'
print(r)

Related

How to print or save all data in Python?

I have an output that contains a lot of lines and I have a problem that it reads only the first and last lines.
I also tried to save them to a text file and it was the same result
Can anyone advise me how to fix this?
This is my output:
-12 -23 0
-13 -24 0
-14 -25 0
...
-119 -130 0
-120 -131 0
-121 -132 0
This code gives the data:
with open('emotion_file.txt', mode='w',newline='\n') as emotion_file:
emotion_file.write(osem)
emotion_file.close()
the code retrieves numbers from demofile.txt. It transforms data into fields and works with them
full code:
import os
import sys
import numpy as np
import re
import csv
#read data
f = open("demofile.txt", "r")
lines = f.readlines()
p=1
#sys.stdout = open("results.txt", "w")
#preprocessing
for i in list(lines):
if i[0] != '<' and i[0] != '>' and i[0] != '=':
p = str(' '.join(i.split()))
print(p)
else:
#w = i[2:]
w = i.replace("=",'')
w = w.replace(">",'')
w = w.replace("<",'')
#print(w)
w = ', '.join(w.split())
y = i[2]
y=int(y)+1
c=np.array([w])
c1 = [int(i) for i in c[0].replace(" ", "").split(",")]
#transform to array
c1=np.array(c1)
frst=c1[0]
c1=np.delete(c1, 0)
n=len(c1)
sest2=c1
c1=np.array([c1]*frst)
c1=np.transpose(c1)
left1 = np.array([[(p + j) * 11 for j in range(frst)]] * n) + c1
left2 = np.array([[(p + j) * -11 for j in range(frst)]] * n) + c1
left=np.where(c1 > 0,left1 , left2)
#print(left)
left=left*-1
b=np.all(left < 0)
#6.vzorec
sest1=left
sest = np.zeros((sest1.flatten().shape[0],2))
sest[:,[0]] = sest1.T.flatten()[:,None]
sest[:,[1]] = np.tile(sest2,frst)[:,None]
sest=str(sest).replace("[",'')
sest=str(sest).replace("]",' 0')
sest = sest[:-1]
sest=str(sest).replace(".",'')
sest=str(sest).replace("\n ",'\n')
sest=str(sest).replace(" ",' ')
if b==False:
sest=str(sest).replace(" ",' ')
else:
sest=str(sest).replace(" ",' ')
sest=str(sest).replace("\n ",'\n')
sest=str(sest).lstrip()
#7.vzorec
sedem=np.transpose(left)*-1
sedem=str(sedem).replace("[",'')
sedem=str(sedem).replace("]",' 0')
sedem=str(sedem).replace(".",'')
sedem = sedem[:-1]
sedem=str(sedem).replace("\n ",'\n')
sedem=str(sedem).replace(" ",' ')
sedem=str(sedem).replace("\n ",'\n')
sedem=str(sedem).lstrip()
#8.vzorec
if frst==1:
osem='---nic-----'
else:
osem=left
osem = np.vstack([ np.c_[left[:,x], left[:,y]]
for x, y in np.c_[np.triu_indices(n=left.shape[1], k=1)] ])
osem=str(osem).replace("[",'')
osem=str(osem).replace("]",' 0')
osem = osem[:-1]
osem=str(osem).replace("\n ",'\n')
osem=str(osem).replace(" ",' ')
osem=str(osem).replace("\n ",'\n')
osem=str(osem).lstrip()
sedem=str(sedem).replace(" ",'')
#for iin osem range
p +=frst
#print(sest)
#print(sedem,)
print(osem,'\n')
with open('emotion_file.txt', mode='w',newline='\n') as emotion_file:
emotion_file.write(osem)
emotion_file.close()
variables sest, sedem and osem transform the fields into different forms
demofile.txt
=>11 1 2 3 4 5 6 7 8 9 10 11
Depending on what kind of data structure osem is, as that could affect the implementation of your for loop, I found that this worked for me:
osem = [3,4,5]
emotion_file = open('emotion_file.txt', 'a')
for i in osem:
emotion_file.write(f'{i}\n')
emotion_file.close()
if osem is a dictionary then the following works as well:
osem = {'key_1': 1, 'key_2': 2}
emotion_file = open('emotion_file.txt', 'a')
for key,value in osem.items():
emotion_file.write(f'{key}\t{value}\n')
emotion_file.close()
If you wish to delete the current data in the emotion_file.txt file, replace 'a' with a 'w' in the open(...) command.
Hopefully this helps!

Data sorting, combining 2 lines

I have a file looking this way:
;1;108/1;4, 109
;1;51;4, 5
;2;109/2;4, 5
;2;108/2;4, 109
;3;108/2;4, 109
;3;51;4, 5
;4;109/2;4, 5
;4;51;4, 5
;5;109/2;4, 5
;5;40/6;5, 6, 7
where
;id1;id2;position_on_shelf_id2
;id1;id3;position_on_shelf_id3
as a result, i want to get:
id1;id2-id3;x
where x are common shelf positions for both id2 and id3, it should look like this
1;108/1-51;4
2;109/2-108/2;4
3;108/2-51;4
4;109/2-51;4, 5
5;109/2-40/6;5
my script works fine up to the moment where I need to type common shelf positions. I tried using .intersection, but it is not working properly, when I have positions consisting of double characters (pos:144-result: 14; pos:551, result: 51; pos:2222-result: 2 i.e)
result = id2_chars.intersection(id3_chars)
any fix for intersection? or maybe some better method on your mind?
code so far:
part1 - merge every 2nd line together
exp = open('output.txt', 'w')
with open("dane.txt") as f:
content = f.readlines()
strng = ""
for i in range(1,len(content)+1):
strng += content[i-1].strip()
if i % 2 == 0:
exp.writelines(strng + '\n')
strng = ""
exp.close()
part2 - intersection:
exp = open('output2.txt', 'w')
imp = open('output.txt')
for line in imp:
none, lp1, dz1, poz1, lp2, dz2, poz2 = line.split(';')
s1 = poz1.lower()
s2 = poz2.lower()
s1_chars = set(s1)
s2_chars = set(s2)
result = s1_chars.intersection(s2_chars)
result = str(result)
exp.writelines(lp1 + ';' + dz1 + '-' + dz2 + ';' + result + '\n')
exp.close()
** i did not filtered the result for my needs yet (it is in "list" form), but it won't be a problem once I get the right intersection result
Your main problem is that you try to intersect 2 sets of characters while you should intersect positions. So you should at least use:
...
s1 = poz1.lower()
s2 = poz2.lower()
s1_poz= set(x.strip() for x in s1.split(','))
s2_poz = set(x.strip() for x in s1.split(','))
result = s1_poz.intersection(s2_poz)
result = ', '.join(result)
...
But in fact, you could easily do the whole processing in one single pass:
exp = open('output.txt', 'w')
with open("dane.txt") as f:
old = None
for line in f: # one line at a time is enough
line = line.strip()
if old is None: # first line of a block, just store it
old = line
else: # second line of a bock, process both
none, lp1, dz1, poz1 = old.split(';')
none, lp2, dz2, poz2 = line.split(';')
poz1x = set(x.strip() for x in poz1.tolower().split(','))
poz2x = set(x.strip() for x in poz2.tolower().split(','))
result = ', '.join(poz1x.intersection(poz2x))
exp.write(lp1 + ';' + dz1 + '-' + dz2 + ';' + result + '\n')
old = None

Writing a list from a for loop into a csv

I wrote a for loop that iterates through a CSV to get a list like this:
[t1, s1]
[t2, s2]
[t3, s3]
and so 4 thousand times.
Now I need to write these into a new CSV file, where they'd populate 2 fields and be separated by a comma.
When I enter this, I only get the last list from the last loop, and with one character in a cell.
def sentiment_analysis():
fo = open("positive_words.txt", "r")
positive_words = fo.readlines()
fo.close()
positive_words = map(lambda positive_words: positive_words.strip(), positive_words)
fo = open("negative_words.txt", "r")
negative_words = fo.readlines()
fo.close()
negative_words = map(lambda negative_words: negative_words.strip(), negative_words)
fo = open("BAC.csv", "r")
data = fo.readlines()
fo.close()
data = map(lambda data: data.strip(), data)
x1 = 0 #number of bullish
x2 = 0 #number of bearish
x3 = 0 #number of unknown
for info in data:
data_specs = info.split(',')
time_n_date = data_specs[0]
sentiment = data_specs[2]
'''Possibly precede with a nested for loop for data_specs???'''
if sentiment == 'Bullish':
'''fo.write(time + ',' + 'Bullish' + '\n')'''
elif sentiment == 'Bearish':
''' fo.write(time + ',' + 'Bearish' + '\n')'''
else:
x3 += 1
positive = 0
negative = 0
content_words = data_specs[1].split()
for a in positive_words:
for b in content_words:
if (a == b):
positive = positive + 1
for c in negative_words:
for d in content_words:
if (c == d):
negative = negative + 1
if positive > negative:
'''fo.write(time + ',' + 'Bullish' + '\n')'''
sentiment = 'Bullish'
elif positive < negative:
sentiment = 'Bearish'
else:
sentiment = 'Neutral'
bac2data = [time_n_date, sentiment]
print bac2data
fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
for x in bac2data:
w = csv.writer(fo, delimiter = ',')
w.writerows(x)
fo.close()
My for loop isn't going through it all.
In your code bac2data = [time_n_date, sentiment] creates a list containing 2 string items. The proper way to write that to a CSV file with csv.writer() is with writerow(bac2data).
The last part of your code contains a number of errors. Firstly you are opening the CSV file in write mode ('w') for every line of the incoming data. This will overwrite the file each time, losing all data except the last line. Then you are iterating over the bac2data list and calling writerows() on each item. That's going to write each character from the string on it's own line (which matches your reported output).
Instead, open the output file and create a csv.writer outside of the main for info in data: loop:
fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
writer = csv.writer(fo)
for info in data:
....
Then replace these lines at the bottom of the main loop:
bac2data = [time_n_date, sentiment]
print bac2data
fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
for x in bac2data:
w = csv.writer(fo, delimiter = ',')
w.writerows(x)
fo.close()
with this:
bac2data = [time_n_date, sentiment]
print bac2data
writer.writerow(bac2data)
Once you have that working, and no longer need to print bac2data for debugging, you can just use 1 line:
writer.writerow((time_n_date, sentiment)]
Update
Complete code for function:
def sentiment_analysis():
fo = open("positive_words.txt", "r")
positive_words = fo.readlines()
fo.close()
positive_words = map(lambda positive_words: positive_words.strip(), positive_words)
fo = open("negative_words.txt", "r")
negative_words = fo.readlines()
fo.close()
negative_words = map(lambda negative_words: negative_words.strip(), negative_words)
fo = open("BAC.csv", "r")
data = fo.readlines()
fo.close()
data = map(lambda data: data.strip(), data)
x1 = 0 #number of bullish
x2 = 0 #number of bearish
x3 = 0 #number of unknown
fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
writer = csv.writer(fo)
for info in data:
data_specs = info.split(',')
time_n_date = data_specs[0]
sentiment = data_specs[2]
'''Possibly precede with a nested for loop for data_specs???'''
if sentiment == 'Bullish':
'''fo.write(time + ',' + 'Bullish' + '\n')'''
elif sentiment == 'Bearish':
''' fo.write(time + ',' + 'Bearish' + '\n')'''
else:
x3 += 1
positive = 0
negative = 0
content_words = data_specs[1].split()
for a in positive_words:
for b in content_words:
if (a == b):
positive = positive + 1
for c in negative_words:
for d in content_words:
if (c == d):
negative = negative + 1
if positive > negative:
'''fo.write(time + ',' + 'Bullish' + '\n')'''
sentiment = 'Bullish'
elif positive < negative:
sentiment = 'Bearish'
else:
sentiment = 'Neutral'
bac2data = [time_n_date, sentiment]
print bac2data
writer.writerow(bac2data)
fo.close()

Reading each line form a text file and determining if it has another letter

I have a text file that looks like this:
A 12 YDUSD ASSDAS FSDDSFSD SDFF
AA FSDFSD FSDF SDFSDG GSDDSFS SDF
AB SDFSDF SDFFSDFDS SDSDSDSDS
ACC SDFSDDSDFSD EW12 SDFSD 3322
ACDD FDSDSFS SDFGSDG DSGSDF
AB FSDFSD SDF34 223DSFSD
ABBD 2332 ADSDFDSFDS
And so on and so fourth for about 500 different beginnings to each line. I want to write a program that will get the line, take everything from it before the first tab (there is a tab between each column) and put it into a list like this:
['A', 'AA', 'AB', 'ACC', 'ACDD', 'AB', 'ABBD']
This is my program so far but it doesn't quite work:
file1 = open("filename", "r")
file2 = open("filename2", "w")
i=0
k = 0
sp500list = []
with open("filename1") as f:
lines = f.readlines()
while (abc < len(lines)):
LineStr = str(lines[i])
j = 0
if (LineStr[j] != ''):
j = j + 1
if (LineStr[j] !=''):
j = j + 1
elif (LineStr[j] == ' '):
sp500list.append(str(LineStr[:2]))
i = i + 1
if (LineStr[j] !=''):
j = j + 1
elif (LineStr[j] == ' '):
sp500list.append(str(LineStr[:3]))
i = i + 1
if (LineStr[j] !=''):
sp500list.append(str(LineStr[:4]))
i = i + 1
j = 0
elif (LineStr[j] == ' '):
i = i + 1
print sp500list
abc = abc + 1
So far all it does is return an empty array, can anyone help?
Thanks!
This can be simplified. split each line by '\t', and take the first element of the resulting list.
>>> with open('file.txt') as f:
... result = [line.split('\t', 1)[0] for line in f]
...
>>> result
['A', 'AA', 'AB', 'ACC', 'ACDD', 'AB', 'ABBD']
Alternatively, use result = [line[:line.find('\t')] for line in f].

Error in python replace. (AttributeError: 'tuple' object has no attribute 'replace')

Environment
I am using Python 3 and my OS is Windows 7.
I understand that some commands have changed from the transition from python 2.7 to 3 (What I have used).
Problem
The variable is temporary but is this:
(((((0, 7), 7), 8), 4), 5)
Here is the code to get rid of the brackets:
randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")
When it tries to execute the replace function, I get thrown an error:
Traceback (most recent call last):
File "<string>", line 248, in run_nodebug
File "Criptic.py", line 134, in <module>
randy = randy.replace(")", "")
AttributeError: 'tuple' object has no attribute 'replace'
Edit:
Here is all of the code:
#Import string
import string
import random
#Input user data
text = input("Enter your text to be converted: ")
#Test Print
print("-------------------------")
print("Your text is: ",text)
#Break up all the data
data = list(text)
#Lowercase
count = text.count("a")
count1 = text.count("b")
count2 = text.count("c")
count3 = text.count("d")
count4 = text.count("e")
count5 = text.count("f")
count6 = text.count("g")
count7 = text.count("h")
count8 = text.count("i")
count9 = text.count("j")
count10 = text.count("k")
count11 = text.count("l")
count12 = text.count("m")
count13 = text.count("n")
count14 = text.count("o")
count15 = text.count("p")
count16 = text.count("q")
count17 = text.count("r")
count18 = text.count("s")
count19 = text.count("t")
count20 = text.count("u")
count21 = text.count("v")
count22 = text.count("w")
count23 = text.count("x")
count24 = text.count("y")
count25 = text.count("z")
count26 = text.count("A")
#Uppercase
count27 = text.count("B")
count28 = text.count("C")
count29 = text.count("D")
count30 = text.count("E")
count31 = text.count("F")
count32 = text.count("G")
count33 = text.count("H")
count34 = text.count("I")
count35 = text.count("J")
count36 = text.count("K")
count37 = text.count("L")
count38 = text.count("M")
count39 = text.count("N")
count40 = text.count("O")
count41 = text.count("P")
count42 = text.count("Q")
count43 = text.count("R")
count44 = text.count("S")
count45 = text.count("T")
count46 = text.count("U")
count47 = text.count("V")
count48 = text.count("W")
count49 = text.count("X")
count50 = text.count("Y")
count51 = text.count("Z")
#Other Characters
count52 = text.count(" ")
count53 = text.count("?")
count54 = text.count("#")
count55 = text.count("(")
count56 = text.count(")")
count57 = text.count(".")
#Numbers
count58 = text.count("1")
count59 = text.count("2")
count60 = text.count("3")
count61 = text.count("4")
count62 = text.count("5")
count63 = text.count("6")
count64 = text.count("7")
count65 = text.count("8")
count66 = text.count("9")
count67 = text.count("0")
#Counting how many characters in the sentence
finalcount = count + count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9 + count10 + count11 + count12 + count13 + count14 + count15 + count16 + count17 + count18 + count19 + count20 + count21 + count22 + count23 + count24 + count25 + count26 + count27 + count28 + count29 + count31 + count32 + count33 + count34 + count35 + count36 + count37 + count38 + count39 + count40 + count41 + count42 + count43 + count44 + count45 + count46 + count47 + count48 + count49 + count50 + count51 + count52 + count53 + count54 + count55 + count56 + count57 + count58 + count59 + count60 + count61 + count62 + count63 + count64 + count65 + count66 + count67
#Final count of Characters
print(" Chars: ",finalcount)
print("-------------------------")
char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!##$%^&*()?_+-=1234567890 "
charnum = 74 + 11
list(char)
randy = 0
num = 0
while num < finalcount:
rand = random.randrange(1,9)
randy = randy,rand
finalcount = finalcount - 1
if rand == 1:
print(text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 2:
print(char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 3:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 4:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 5:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 6:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 7:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 8:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],",")
if rand == 9:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],",")
randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")
print("-------------------------")
print("::::::::Completed::::::::")
print("-------------------------")
print(randy)
print("-This is your code unlock key. Without this code it will not decript!!!! ")
print("-------------------------")
You have a tuple (a sort of list) of numbers, and you want to make that a string. You can't replace the brackets, they aren't a part of the variable, they are just a part of its representation.
Instead you should use join() and str()
result = " ".join(str(x) for x in randy)
However, this probably will not give the right result either, as it is a nested list of tuples. You probably mean: randy = randy + (rand,) instead if randy = randy,rand.
randy = randy,rand
This notation creates a tuple of two elements, tuples do not have a method called replace.
If you just want to concatenate the numbers into a list representation separated by commas, you could do this in your loop:
randy = []
num = 0
while num < finalcount:
rand = random.randrange(1,9)
randy.append(rand)
Then you remove the replace lines, and instead do this
randy = ",".join(randy)
to get a string that has the values separated by commas.
You should maybe add the output that you would like to get to your question, because at the moment it's not quite clear what you want to do exactly.
exam_st_date = (11, 12, 2014)
# Sample Output : The examination will start from : 11 / 12 / 2014
newtup = str(exam_st_date).replace(',', ' /')
print(newtup)
look at mine example should give you some results
To delete a list of characters, you could use the string's translate method:
import string
name = "(((((0, 7), 7), 8), 4), 5)"
table = string.maketrans( '', '', )
print name.translate(table,"()")

Categories