f = open('f.txt', 'r')
import re
for line in f:
u = line.split(':')[0]
p = line.split(':')[1]
m = re.search(r'\d+$', u)
if m is not None:
m1 = re.search(r'\d+',p)
if m1 is None:
print(u + ':' + p.rstrip() + m.group())
f.close()
f = open('f.txt', 'r')
for line in f:
l = line.rstrip().split(':')[1]
m = re.search(r'\d+', l)
if m is None:
print(line.rstrip() + '123')
f.close()
f = open('f.txt', 'r')
for line in f:
l = line.rstrip().split(':')[1]
m = re.search(r'\d+', l)
if m is None:
print(line.rstrip() + '1')
f.close()
f = open('f.txt', 'r')
for line in f:
u = line.split(':')[0]
p = line.split(':')[1]
m = re.search(r'\d+$', u)
if m is not None and len(m.group()) == 4:
if int(m.group()) < 2013 and int(m.group()) > 1950:
m1 = re.search(r'\d+$',p)
if m1 is None:
print(u + ':' + p.rstrip() + m.group()[2:])
f.close()
f = open('f.txt', 'r')
for line in f:
s = line.split(':')[1]
m = re.search(r'\d+', s)
if m is not None:
newone = line.split(':')[0] + ':' + re.sub(r'\d+',"", s).rstrip()
if newone[-1:] != ':':
print(line.split(':')[0] + ':' + re.sub(r'\d+',"", s).rstrip())
f.close()
this is my .py scrit I had made, It works fine but it doesnt export a .txt once it has finished editing all of the lines in f.txt - it just closes
Could I get some help in adding some code to make it export as finished.txt - thanks in advance
You are not opening a file in writing mode in any part of the code.
You do it like this:
e = open('filename.txt', 'w')
Then, write lines to it:
e.write('example string')
Related
I have a file named input.txt.
name="XYZ_PP_0" number="0x12" bytesize="4" info="0x00000012"
name="GK_LMP_2_0" number="0xA5" bytesize="8" info="0x00000000bbae321f"
name="MP_LKO_1_0" number="0x356" bytesize="4" info="0x00000234"
name="PNP_VXU_1_2_0" number="0x48A" bytesize="8" info="0x00000000a18c3ba3"
name="AVU_W_2_3_1" number="0x867" bytesize="1" info="0x0b"
From this file i need to create another file output.txt. in this file if bytesize="8" then info is split in to two.
eg:
number="0x48A" bytesize="8" info="0x00000000a18c3ba3"
then output file will contain:
name="PNP_VXU_1_2_0_LOW" number="0x48A" info="0xa18c3ba3"
name="PNP_VXU_1_2_0_HIGH" number="0x4BC" info="0x00000000"
where 0x4BC = 0x48A + 0x32
this is current code:
import re
infile_path = "./input.txt"
outfile_path = "./output.txt"
with open(infile_path, "r") as infile, open(outfile_path, "w") as outfile:
for s in infile:
r = re.match('number="(.*)" bytesize="(.*)" info="(.*)"', s)
if r:
num, bs, info = map(lambda x: int(x, 0), r.groups())
l = len(r.group(3)) - 2
if bs == 8:
l = 8
nums = (num, num + 0x32)
infos = (info % (2**32), info // (2**32))
else:
nums = (num, )
infos = (info, )
for num, info in zip(nums, infos):
outfile.write(f'number="{num:#x}" info="{info:#0{l+2}x}"\n')
output.txt for this code:
number="0x12" info="0x00000012"
number="0xA5" info="0xbbae321f"
number="0xD7" info="0x00000000"
number="0x356" info="0x00000234"
number="0x48A" info="0xa18c3ba3"
number="0x4BC" info="0x00000000"
number="0x867" info="0x0b"
Expected Output:
name="XYZ_PP_0" number="0x12" info="0x00000012"
name="GK_LMP_2_0_LOW" number="0xA5" info="0xbbae321f"
name="GK_LMP_2_0_HIGH"number="0xD7" info="0x00000000"
name="MP_LKO_1_0" number="0x356" info="0x00000234"
name="PNP_VXU_1_2_0_LOW" number="0x48A" info="0xa18c3ba3"
name="PNP_VXU_1_2_0_HIGH" number="0x4BC" info="0x00000000"
name="AVU_W_2_3_1" number="0x867" info="0x0b"
How can i add the LOW and HIGH to the name if byte size is 8?
With your current code you just need to capture the name with the regex and add it to the output in the same way you handle nums:
with open(infile_path, "r") as infile, open(outfile_path, "w") as outfile:
for s in infile:
r = re.match('name="(.*)" number="(.*)" bytesize="(.*)" info="(.*)"', s)
if r:
name, *numbers = r.groups()
num, bs, info = map(lambda x: int(x, 0), numbers)
l = len(r.group(3)) - 2
if bs == 8:
l = 8
names = (f'{name}_LOW', f'{name}_HIGH')
nums = (num, num + 0x32)
infos = (info % (2**32), info // (2**32))
else:
names = (name, )
nums = (num, )
infos = (info, )
for name, num, info in zip(names, nums, infos):
outfile.write(f'name="{name}" number="{num:#x}" info="{info:#0{l+2}x}"\n')
The regex is compiled at the beginning to improve performance.
The process() function analyzes an input line and writes to the output file.
PATTERN = re.compile(r'(\w*)="([^"]*)')
def process(line, f_out):
name, number, bytesize, info = [x[1] for x in PATTERN.findall(line)]
if int(bytesize) == 8:
n = int(number, 16) + 0x32
f_out.writelines(
[
f'name="{name}_LOW" number="{number}" info="0x{info[-8:]}"\n',
f'name="{name}_HIGH" number="0x{n:02X}" info="{info[:10]}"\n'
]
)
else:
f_out.writelines(line)
with open("input.txt") as f_in, open("output.txt", "w") as f_out:
for line in f_in:
process(line, f_out)
Not very good at programming. Need to change the script code. It is necessary to read the contents of the file and split it into separate keys, and then save them in a file. Thank you in advance for your help!!!
'text.txt'
File:
0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5
0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278
022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc
021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72
0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e
025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4
02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed
import binascii
with open('text.txt') as f:
text = f.read()
compressed_key_hex = text.split('\n')
computed_uncompressed_key = []
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
x_hex = compressed_key_hex[2:66]
x = int(x_hex, 16)
prefix = compressed_key_hex[0:2]
y_square = (pow(x, 3, p) + 7) % p
y_square_square_root = pow(y_square, (p+1)//4, p)
if prefix == "02":
y = (-y_square_square_root) % p
else:
y = y_square_square_root
computed_y_hex = hex(y)[2:66]
computed_uncompressed_key = "04" + x_hex + computed_y_hex
with open('result.txt', 'w') as f:
f.write('\n'.join(computed_uncompressed_key))
I get the error:
===================== RESTART: D:\detailALL\03\Bit06.py =====================
Traceback (most recent call last):
File "D:\detailALL\03\Bit06.py", line 12, in <module>
x = int(x_hex, 16)
TypeError: int() can't convert non-string with explicit base
>>>
You are passing a list rather than a str. In the following code x_hex is a list.
x_hex = compressed_key_hex[2:66]
So you need to convert the list to str, you can do that using the following:
x_hex = ''.join(compressed_key_hex[2:66])
I guess the following might be your required solution:
import binascii
with open('text.txt') as f:
text = f.read()
compressed_key_hex = text.split('\n')
print(compressed_key_hex)
computed_uncompressed_key_list = []
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
for val in compressed_key_hex:
x_hex = val[2:66]
x = int(x_hex, 16)
prefix = val[0:2]
y_square = (pow(x, 3, p) + 7) % p
y_square_square_root = pow(y_square, (p+1)//4, p)
if prefix == "02":
y = (-y_square_square_root) % p
else:
y = y_square_square_root
computed_y_hex = hex(y)[2:66]
computed_y_hex = computed_y_hex.zfill(64)
computed_uncompressed_key = "04" + x_hex + computed_y_hex
computed_uncompressed_key_list.append(computed_uncompressed_key)
with open('result.txt', 'w') as f:
f.write('\n'.join(computed_uncompressed_key_list))
text.txt file:
0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5
0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278
022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc
021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72
0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e
025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4
02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed
result.txt file:
0400e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5c9b034d2aa9ee1ef7b2346e8fc9c0245a8746a92bfdbb472fc98397477551ced
0416b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e12780126dfa95d2d9ab8fc055ce158f1d2ef51c2a012413b3f88a6365f375cf903f8
042bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbcf4a39790075ce43dc08fbf0ecc9cc732415e6b066c3b8b8d960b8548e8a612b7
041060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd723f873c976d071939edf8450124da64c3d9a1b35fb070761b01a5bace7d741588
0418cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514efb4f8645c503e7a39954e977f7af8e802a5ec44ce3084cb6fb4e133a79733e77
045d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4e7c279c9d2c3e731803a4dde91a0d9409e49b1cbec3c7ac536a3783d9518d737
04264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efeddbd8a97a8762f6a1add1ea6f549b61316fe675fc703d49f597a91ad620f7627a
I'm relatively new to python and was wondering if I could get some assistance in parsing data so that it is easier to analyze.
My data is in the following form (each is an entire line):
20160930-07:06:54.481737|I|MTP_4|CL:BF K7-M7-N7 Restrict for maxAggressive: -4.237195
20160930-07:06:54.481738|I|MTP_4|CL:BF K7-M7-N7 BidPrice: -5.0 mktBestBid: -5.0 bidTheo: -4.096774 bidSeedEdge: 0.195028 bidUnseedEdge: CL:BF K7-M7-N7 = 0.14042 Min Bid: -6.0 Max Ticks Offset: 1 Max Aggressive Ticks: 1
This is my code so far
# Output file
output_filename = os.path.normpath("Mypath/testList.log")
# Overwrites the file
with open(output_filename, "w") as out_file:
out_file.write("")
# Open output file
with open(output_filename, "a") as out_file:
# Open input file in 'read' mode
with open("mypath/tradedata.log", "r") as in_file:
# Loop over each log line, Grabs lines with necessary data
for line in islice(in_file, 177004, 8349710):
out_file.write(line)
Would it be easiest to just go through and do it by keywords like; bidUnseedEdge, mktBesdBid, etc. ?
infilename = "path/data.log"
outfilename = "path/OutputData.csv"
with open(infilename, 'r') as infile,\
open(outfilename, "w") as outfile:
lineCounter = 0
for line in infile:
lineCounter += 1
if lineCounter % 1000000 == 0:
print lineCounter
data = line.split("|")
if len(data) < 4:
continue
bidsplit = data[3].split("bidTheo:")
namebid = data[3].split("BidPrice:")
if len(bidsplit) == 2:
bid = float(bidsplit[1].strip().split()[0])
bidname = namebid[0].strip().split(",")[0]
#print "bidTheo," + data[0] + "," + str(bid)
outfile.write("bidTheo," + data[0] + "," + bidname + "," + str(bid) + "\n")
offersplit = data[3].split("offerTheo:")
nameoffer = data[3].split("AskPrice:")
if len(offersplit) == 2:
offer = float(offersplit[1].strip().split()[0])
offername = nameoffer[0].strip().split(",")[0]
#print "offerTheo," + data[0] + "," + str(offer)
outfile.write("offerTheo," + data[0] + "," + offername + "," + str(offer) + "\n")
print "Done"
Here's the code. It runs for usually somewhere less than an hour and stops giving out the notifications or writing anything to the log file. The code is run as a Startup application on Ubuntu 16.04. I have tried adding exceptions for all kinds of errors in the hope of understanding why the program might be stopping, but to no avail. It just stops. And running it from the terminal used to work earlier, but even that has stopped giving results after a period of time.
import urllib2, os, time, sys, re, pynotify
from bs4 import BeautifulSoup
from os import listdir
from os.path import isfile, join
from random import sample
from datetime import datetime
savepath = '/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/'
count = -1
iflp = True
innerct = 0
rndct = 0
rndct2 = 0
flag = False
try:
while(count < 1000):
time.sleep(3)
count = (count+1) % 30
url = "http://www.insightsonindia.com/category/current-affairs-2"
connect = False
netcnct = True
itntchk = 0
while not connect:
try:
text = urllib2.urlopen('http://www.insightsonindia.com/category/current-affairs-2').read()
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/log.txt', "a") as file:
file.write("{:%B %d, %Y - %H. %M. %S}".format(datetime.now()) + '\t\tIndicator of Something\n')
connect = True
except urllib2.URLError as e:
itntchk = itntchk + 1
if (itntchk > 20):
netcnct = False
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/log.txt', "a") as file:
file.write("{:%B %d, %Y - %H. %M. %S}".format(datetime.now()) + '\t\tInternet Connection Issues\n')
pass
if (netcnct):
soup = BeautifulSoup(text, "lxml")
data = soup.findAll('div',attrs={'class':'post-thumbnail'})
if count == 0:
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/log.txt', "a") as file:
file.write("{:%B %d, %Y - %H. %M. %S}".format(datetime.now()) + '\t\tConnection Established After Sleep\n')
for div in data:
links = div.findAll('a')
for a in links:
if os.path.exists(savepath + 'urlist.txt'):
with open(savepath + 'urlist.txt', "r+") as file:
line_found = any(a['href'] in line for line in file)
if not line_found:
file.seek(0, os.SEEK_END)
connected = False
while not connected:
try:
html = urllib2.urlopen(a['href']).read()
connected = True
except urllib2.URLError as e:
pass
else:
file.write(a['href'] + '\n')
sp = BeautifulSoup(html, "lxml")
txt = sp.get_text()
realtxt = txt.encode('utf-8')
s2 = "print PDFInsights "
s3 = "Please Share This Post If You"
dtxt = realtxt[realtxt.index(s2) + len(s2):realtxt.index(s3)]
if ('Current' in a.get('title')):
p1 = [m.start() for m in re.finditer('Paper 1 Topic', dtxt)]
p2 = [m.start() for m in re.finditer('Paper 2 Topic', dtxt)]
p3 = [m.start() for m in re.finditer('Paper 3 Topic', dtxt)]
p4 = [m.start() for m in re.finditer('Paper 4 Topic', dtxt)]
pre = [m.start() for m in re.finditer('Facts for Prelims', dtxt)]
onlyfiles = [f for f in listdir('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/') if isfile(join('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/', f))]
ltvr = []
while(len(p4)>0):
ltvr.append(min(p4))
p4.remove(min(p4))
while(len(p3)>0):
ltvr.append(min(p3))
p3.remove(min(p3))
while(len(p2)>0):
ltvr.append(min(p2))
p2.remove(min(p2))
while(len(p1)>0):
ltvr.append(min(p1))
p1.remove(min(p1))
while(len(pre)>0):
ltvr.append(min(pre))
pre.remove(min(pre))
i = 0
ltvr.sort()
while (i < len(ltvr)):
if(dtxt[ltvr[i]+6]=='1'):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Paper1/' + a.get('title') + '.txt', "a") as file:
file.write(dtxt[ltvr[i]:ltvr[i+1]] + '\n')
elif(dtxt[ltvr[i]+6]=='2'):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Paper2/' + a.get('title') + '.txt', "a") as file:
file.write(dtxt[ltvr[i]:ltvr[i+1]] + '\n')
elif(dtxt[ltvr[i]+6]=='3'):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Paper3/' + a.get('title') + '.txt', "a") as file:
file.write(dtxt[ltvr[i]:ltvr[i+1]] + '\n')
elif(dtxt[ltvr[i]+6]=='4'):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Paper4/' + a.get('title') + '.txt', "a") as file:
file.write(dtxt[ltvr[i]:ltvr[i+1]] + '\n')
elif(dtxt[ltvr[i]+6]=='f'):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/' + str(len(onlyfiles)+1) + '.txt', "w+") as file:
file.write(dtxt[ltvr[i]:])
i = i + 1
elif ('Editorial' in a.get('title')):
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Editorials/' + a.get('title') + '.txt', "w+") as file:
file.write(dtxt)
else:
with open(savepath + 'urlist.txt', "w+") as file:
file.write(a['href'] + '\n')
if ((count == 0) or (netcnct == False)):
innerct = (innerct + 1) % 2
if (netcnct == False):
innerct = 0
if (innerct == 0):
pynotify.init("Basic")
onlyfiles = [f for f in listdir('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/') if isfile(join('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/', f))]
Tr = True
Tre = True
while (Tr):
if (rndct == 0):
rndn = sample(xrange(1, len(onlyfiles)), len(onlyfiles) - 1)
try:
filename = '/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/Pre/' + str(rndn[rndct]) + '.txt'
if (flag):
rndct = (rndct + 1) % (len(onlyfiles) - 1)
flag = False
lines = open(filename).read().splitlines()
if (rndct2 == 0):
rnd2 = sample(xrange(1, len(lines)), len(lines) - 1)
while (Tre):
chk = rndct2
rndct2 = (rndct2 + 1) % (len(lines) - 1)
if(rndct2 < chk):
flag = True
try:
if (lines[rnd2[rndct2]][0].isalnum()):
Tre = False
except IndexError:
pass
Tr = False
except (IOError,IndexError):
pass
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/log.txt', "a") as file:
file.write("{:%B %d, %Y - %H. %M. %S}".format(datetime.now()) + '\tFileCt\t' + str(rndct) + '\t' + str(rndn[rndct]) + '\tLineCt\t' + str(rndct2) + '\t' + str(rnd2[rndct2]) + '\n')
n = pynotify.Notification("Fun Facts",
lines[rnd2[rndct2]]
)
n.show()
except Exception:
with open('/mnt/8E1C30331C301923/PersonalAthul/CS/Insights/Current/log.txt', "a") as file:
file.write("{:%B %d, %Y - %H. %M. %S}".format(datetime.now()) + '\t\t Sorry bro') #+ sys.exc_info()[0])
Hello all so i've been tasked to count lines and paragraphs. Counting every line is obviously easy but im stuck on counting the paragraphs. If a paragraph has no character it will give back the number zero and for every paragraph is an increment higher. For example an input file is: Input and an Output should come out Output
so my code is:
def insert_line_para_nums(infile, outfile):
f = open(infile, 'r')
out = open(outfile, 'w')
linecount = 0
for i in f:
paragraphcount = 0
if '\n' in i:
linecount += 1
if len(i) < 2: paragraphcount *= 0
elif len(i) > 2: paragraphcount = paragraphcount + 1
out.write('%-4d %4d %s' % (paragraphcount, linecount, i))
f.close()
out.close()
def insert_line_para_nums(infile, outfile):
f = open(infile, 'r')
out = open(outfile, 'w')
linecount = 0
paragraphcount = 0
empty = True
for i in f:
if '\n' in i:
linecount += 1
if len(i) < 2:
empty = True
elif len(i) > 2 and empty is True:
paragraphcount = paragraphcount + 1
empty = False
if empty is True:
paragraphnumber = 0
else:
paragraphnumber = paragraphcount
out.write('%-4d %4d %s' % (paragraphnumber, linecount, i))
f.close()
out.close()
This is one way to do it, and not the prettiest.
import re
f = open('a.txt', 'r')
paragraph = 0
lines = f.readlines()
for idx, line in enumerate(lines):
if not line == '\n':
m = re.search(r'\w', line)
str = m.group(0)
try:
# if the line is a newline, and the previous line has a str in it, then
# count it as a paragraph.
if line == '\n' and str in lines[idx-1]:
paragraph +=1
except:
pass
if lines[-1] != '\n': # if the last line is not a new line, count a paragraph.
paragraph +=1
print paragraph