Related
I applied a previous code for a log, to get the following list
log = ['',
'',
'ABC KLSC: XYZ',
'',
'some text',
'some text',
'%%ABC KLSC: XYZ',
'some text',
'',
'ID = 5',
'TME = KRE',
'DDFFLE = SOFYU',
'QWWRTYA = GRRZNY',
'',
'some text',
'-----------------------------------------------',
'',
'QUWERW WALS RUSZ CRORS ELME',
'P <NULL> R 98028',
'P <NULL> R 30310',
'',
'',
'Some text',
'',
'Some text',
'',
'--- FINISH'
]
and I want to filter those lines in order to get a list with only the lines that contains "=" and the
lines that are ordered in columns format (those below headers QUWERW, WALS, RUSZ, CRORS), but additionally, for those lines with column format, store
each value with its corresponding header.
I was able to filter the desired lines with code below (not sure here if there is a better condition to filter the lines with columns)
d1 = [line for line in log if len(line) > 50 or " = " in line]
d1
>>
[
'ID = 5',
'TME = KRE',
'DDFFLE = SOFYU',
'QWWRTYA = GRRZNY',
'QUWERW WALS RUSZ CRORS ELME',
'P <NULL> R 98028',
'P <NULL> R 30310',
]
But I don´t know how to get the output I'm looking for as follows. Thanks for any help
[
'ID = 5',
'TME = KRE',
'DDFFLE = SOFYU',
'QWWRTYA = GRRZNY',
'QUWERW = P',
'WALS = <NULL>',
'RUSZ = R',
'CRORS = 98028',
'QUWERW = P',
'WALS = <NULL>',
'RUSZ = R',
'CRORS = 30310'
]
Finding the = is straight-forward. One way to find the column values might be, as follows, to identify header rows that contain the headings, and then zipping the following rows when splitting by white-space.
items_list = []
for item in log:
if '=' in item:
items_list.append(item)
elif len(item.split()) > 3:
splits = item.split()
if all(header in splits for header in ['QUWERW', 'WALS', 'RUSZ', 'CRORS']):
headers = splits
else:
for lhs,rhs in zip(headers,splits):
items_list.append(f'{lhs} = {rhs}')
print('\n'.join(items_list))
def text_process(text):
text = text.translate(str.maketrans('', '', string.punctuation))
return " ".join(text)
Input text: 'Transaction value was - RS.3456.63 '
Output : 'Transaction value was RS 345663 '
Could someone suggest me how to remove special characters (including '.' ) during text pre-processing but retain the decimal numbers?
Required Output : 'Transaction value was RS 3456.63 '
You can use a more generic regex to replace all special characters except .
import re
def text_process(text):
text = re.sub('[^\w.]+', ' ', text)
return text
s = 'Transaction: value* #was - 3456.63 Rupees'
text_process(s)
You get
'Transaction value was 3456.63 Rupees'
EDIT: The following function returns only the number with decimals.
def text_process(text):
text = re.sub('[^\d.]+', '', text)
return text
s = 'Transaction: value* #was - 3456.63 Rupees'
text_process(s)
'3456.63'
If I understand your question correctly, this code is for you:
text = 'Transaction value was, - 3456.63 Rupees'
regex = r"(?<!\d)[" + string.punctuation + "](?!\d)"
result = re.sub(regex, "", text)
# output: 'Transaction value was 3456.63 Rupees'
To solve your second question, try using this trick:
text = 'Transaction value was, - Rs.3456.63'
regex_space = r"([0-9]+(\.[0-9]+)?)"
regex_punct = r'[^\w.]+'
re.sub(r'[^\w.]+', ' ', re.sub(regex_space,r" \1 ", text).strip())
# output: 'Transaction value was Rs. 3456.63 Rupees'
I need to skip 47 lines of the file(header and so on),then read this
4.163186002791e+04 3.578830331359e+04 3.076496349687e+04 2.644671278966e+04 2.273458304119e+04
1.954349752908e+04 1.680032112209e+04 1.444218412726e+04 1.241504140604e+04 1.067243373686e+04
9.174423035938e+03 7.886677033340e+03 6.779682426302e+03 5.828068476394e+03 5.010025548360e+03
1.737988920100e+03 1.284332855871e+03 1.104060538508e+03 8.158747205330e+02 7.013564117662e+02
6.029121922103e+02 5.182858606802e+02 4.455379022877e+02 2.433020871700e+02 2.091515701348e+02
1.797945089525e+02 1.545580816278e+02 1.328639052196e+02 9.818329499070e+01 7.255514128762e+01
5.361653963401e+01 4.609078195788e+01 3.962135930423e+01 3.406000172766e+01 2.927925083995e+01
2.516953864546e+01 2.163667639887e+01 1.859969593339e+01 1.598899398582e+01 1.374473698894e+01
1.181548977143e+01 1.015703673713e+01 8.731368506527e+00 7.505810795983e+00 6.452275569743e+00
5.546617302183e+00 4.768079596776e+00 4.098819479081e+00 3.523498461194e+00 3.028931005477e+00
2.603782330822e+00 2.238308635635e+00 1.924133783786e+00 1.654057335509e+00 1.421889523591e+00
1.222309392724e+00 1.050742850800e+00 9.032578372386e-01 7.764742057598e-01 6.674862562538e-01
5.737961402745e-01 4.932566139141e-01 3.133421372728e-01 2.315524554696e-01 1.990511474577e-01
1.711118080085e-01 1.470941072881e-01 1.264475938317e-01 1.086990789815e-01 9.344179207682e-02
8.032605785014e-02 6.905128236880e-02 5.935906385039e-02 5.102727046220e-02
possibly as list,and then again skip 21 lines and read the part of the file in same format as presented above.
Firstly my idea was something like this:
from itertools import islice
n=15
with open('91_FULLMERGED.edi') as f:
lines_after_48 = f.readlines()[48:]
while True:
next_15_lines = list(islice(lines_after_48, n))
if not next_15_lines:
break
But that is not working.
milenko#milenko-HP-Compaq-6830s:~/EDIs$ python k1.py
It is just standing still in terminal.
How to solve this?
Code
I use the csv reader cause the format is csv with delimiter ' '. At first I just skipped 47 lines by just next(f,None). Afterwards the csv module is making the trick. If you want to output as a file you can just use the csv writer. You can uncomment the code if you want to remove the empty strings from the output list. But then the output to the file is not similar to the input. It depends on how you wanna use the data.
import csv
with open('input.txt', 'rb') as f:
for i in range(47):
next(f, None)
reader = csv.reader(f,delimiter=' ')
values = list(reader)
# if you want to remove the ''
#for idx, val in enumerate(values):
# values[idx] = [x for x in values[idx] if x != '']
print values
with open('output.txt', 'wb') as f:
writer = csv.writer(f, delimiter=' ', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for line in values:
writer.writerow(line)
Output to screen
[['4.163186002791e+04', '', '3.578830331359e+04', '', '3.076496349687e+04', '', '2.644671278966e+04', '', '2.273458304119e+04'],
['1.954349752908e+04', '', '1.680032112209e+04', '', '1.444218412726e+04', '', '1.241504140604e+04', '', '1.067243373686e+04'],
['9.174423035938e+03', '', '7.886677033340e+03', '', '6.779682426302e+03', '', '5.828068476394e+03', '', '5.010025548360e+03'],
['1.737988920100e+03', '', '1.284332855871e+03', '', '1.104060538508e+03', '', '8.158747205330e+02', '', '7.013564117662e+02'],
['6.029121922103e+02', '', '5.182858606802e+02', '', '4.455379022877e+02', '', '2.433020871700e+02', '', '2.091515701348e+02'],
['1.797945089525e+02', '', '1.545580816278e+02', '', '1.328639052196e+02', '', '9.818329499070e+01', '', '7.255514128762e+01'],
['5.361653963401e+01', '', '4.609078195788e+01', '', '3.962135930423e+01', '', '3.406000172766e+01', '', '2.927925083995e+01'],
['2.516953864546e+01', '', '2.163667639887e+01', '', '1.859969593339e+01', '', '1.598899398582e+01', '', '1.374473698894e+01'],
['1.181548977143e+01', '', '1.015703673713e+01', '', '8.731368506527e+00', '', '7.505810795983e+00', '', '6.452275569743e+00'],
['5.546617302183e+00', '', '4.768079596776e+00', '', '4.098819479081e+00', '', '3.523498461194e+00', '', '3.028931005477e+00'],
['2.603782330822e+00', '', '2.238308635635e+00', '', '1.924133783786e+00', '', '1.654057335509e+00', '', '1.421889523591e+00'],
['1.222309392724e+00', '', '1.050742850800e+00', '', '9.032578372386e-01', '', '7.764742057598e-01', '', '6.674862562538e-01'],
['5.737961402745e-01', '', '4.932566139141e-01', '', '3.133421372728e-01', '', '2.315524554696e-01', '', '1.990511474577e-01'],
['1.711118080085e-01', '', '1.470941072881e-01', '', '1.264475938317e-01', '', '1.086990789815e-01', '', '9.344179207682e-02'],
['8.032605785014e-02', '', '6.905128236880e-02', '', '5.935906385039e-02', '', '5.102727046220e-02']]
Output to file output.txt
4.163186002791e+04 3.578830331359e+04 3.076496349687e+04 2.644671278966e+04 2.273458304119e+04
1.954349752908e+04 1.680032112209e+04 1.444218412726e+04 1.241504140604e+04 1.067243373686e+04
9.174423035938e+03 7.886677033340e+03 6.779682426302e+03 5.828068476394e+03 5.010025548360e+03
1.737988920100e+03 1.284332855871e+03 1.104060538508e+03 8.158747205330e+02 7.013564117662e+02
6.029121922103e+02 5.182858606802e+02 4.455379022877e+02 2.433020871700e+02 2.091515701348e+02
1.797945089525e+02 1.545580816278e+02 1.328639052196e+02 9.818329499070e+01 7.255514128762e+01
5.361653963401e+01 4.609078195788e+01 3.962135930423e+01 3.406000172766e+01 2.927925083995e+01
2.516953864546e+01 2.163667639887e+01 1.859969593339e+01 1.598899398582e+01 1.374473698894e+01
1.181548977143e+01 1.015703673713e+01 8.731368506527e+00 7.505810795983e+00 6.452275569743e+00
5.546617302183e+00 4.768079596776e+00 4.098819479081e+00 3.523498461194e+00 3.028931005477e+00
2.603782330822e+00 2.238308635635e+00 1.924133783786e+00 1.654057335509e+00 1.421889523591e+00
1.222309392724e+00 1.050742850800e+00 9.032578372386e-01 7.764742057598e-01 6.674862562538e-01
5.737961402745e-01 4.932566139141e-01 3.133421372728e-01 2.315524554696e-01 1.990511474577e-01
1.711118080085e-01 1.470941072881e-01 1.264475938317e-01 1.086990789815e-01 9.344179207682e-02
8.032605785014e-02 6.905128236880e-02 5.935906385039e-02 5.102727046220e-02
My Input Values I used
SKIP (for line 1 ... removed 45 Lines for better readablity
SKIP to line 47)
4.163186002791e+04 3.578830331359e+04 3.076496349687e+04 2.644671278966e+04 2.273458304119e+04
1.954349752908e+04 1.680032112209e+04 1.444218412726e+04 1.241504140604e+04 1.067243373686e+04
9.174423035938e+03 7.886677033340e+03 6.779682426302e+03 5.828068476394e+03 5.010025548360e+03
1.737988920100e+03 1.284332855871e+03 1.104060538508e+03 8.158747205330e+02 7.013564117662e+02
6.029121922103e+02 5.182858606802e+02 4.455379022877e+02 2.433020871700e+02 2.091515701348e+02
1.797945089525e+02 1.545580816278e+02 1.328639052196e+02 9.818329499070e+01 7.255514128762e+01
5.361653963401e+01 4.609078195788e+01 3.962135930423e+01 3.406000172766e+01 2.927925083995e+01
2.516953864546e+01 2.163667639887e+01 1.859969593339e+01 1.598899398582e+01 1.374473698894e+01
1.181548977143e+01 1.015703673713e+01 8.731368506527e+00 7.505810795983e+00 6.452275569743e+00
5.546617302183e+00 4.768079596776e+00 4.098819479081e+00 3.523498461194e+00 3.028931005477e+00
2.603782330822e+00 2.238308635635e+00 1.924133783786e+00 1.654057335509e+00 1.421889523591e+00
1.222309392724e+00 1.050742850800e+00 9.032578372386e-01 7.764742057598e-01 6.674862562538e-01
5.737961402745e-01 4.932566139141e-01 3.133421372728e-01 2.315524554696e-01 1.990511474577e-01
1.711118080085e-01 1.470941072881e-01 1.264475938317e-01 1.086990789815e-01 9.344179207682e-02
8.032605785014e-02 6.905128236880e-02 5.935906385039e-02 5.102727046220e-02
give a chance to linecache. and in case of large files, try generators.
import linecache
from sys import argv
def get_specific_lines(f, start, stop):
for i in range(start, stop):
yield linecache.getline(f, i)
script, f, s, e = argv
# check code if (f)ile exists, (s)tart and (e)nd are numeric.
for line in get_specific_lines(f, int(s), int(e)):
print line
$ wc -l asbc.txt
197310 asbc.txt
$ python read.py asbc.txt 144322 144325
Bu yarım dəqiqə ərzində Qleb qabaqcadan dəqiq hesablanmış ləng hərəkətlə əlini qəbulediciyə uzatdı, diktora xırıldamağa imkan vermədən boynunu bururmuş kimi açarın dəstəyini burdu.
Onun azca bundan əvvəlki canlı sifəti yorğun, bozumtul rəng almışdı.
Pryançikovun başı isə başqa problemə qarışmışdı. Hansı gücləndirici kaskadı qoymaq lazım olduğu haqda fikirləşə-fikirləşə o, qayğısız zümzümə edirdi:
I need help understanding how the coloring works for urwid
color_palette = [
('banner', '', '', '', '#fff', 'g35'),
('streak', 'white', '', '', 'g0', 'g35'),
('inside', '', '', '', 'g0', 'g35'),
('outside', '', '', '', 'g0', 'g35'),
('bg', '', '', '', 'g35', '#fff'),]
From the documentation:
http://urwid.org/manual/displayattributes.html#id6
Colors i would like to use:
- #195c60
- #193638
- #232323
USAGE: http://urwid.org/tutorial/index.html#high-color-modes
import urwid
def exit_on_q(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
palette = [
('banner', '', '', '', '#ffa', '#60d'),
('streak', '', '', '', 'g50', '#60a'),
('inside', '', '', '', 'g38', '#808'),
('outside', '', '', '', 'g27', '#a06'),
('bg', '', '', '', 'g7', '#d06'),]
placeholder = urwid.SolidFill()
loop = urwid.MainLoop(placeholder, palette, unhandled_input=exit_on_q)
loop.screen.set_terminal_properties(colors=256)
loop.widget = urwid.AttrMap(placeholder, 'bg')
loop.widget.original_widget = urwid.Filler(urwid.Pile([]))
div = urwid.Divider()
outside = urwid.AttrMap(div, 'outside')
inside = urwid.AttrMap(div, 'inside')
txt = urwid.Text(('banner', u" Hello World "), align='center')
streak = urwid.AttrMap(txt, 'streak')
pile = loop.widget.base_widget # .base_widget skips the decorations
for item in [outside, inside, streak, inside, outside]:
pile.contents.append((item, pile.options()))
loop.run()
It sounds like you want to use the urwid high color example with your custom colors.
In the example from the tutorial, the following line tells terminal to use the 8 bit terminal color mode (https://en.wikipedia.org/wiki/8-bit_color):
loop.screen.set_terminal_properties(colors=256)
To use your custom color, you need to convert your (presumably RGB) color to the closest possible corresponding 8 bit terminal color. Note that you won't necessarily be able to get an exact match: there are way more RGB hex color codes than 8 bit terminal color codes.
I used this tool to do find an approximate match for your colors: https://gist.github.com/MicahElliott/719710
$ colortrans 195c60
RGB 195c60 -> xterm color approx 23 (005f5f)
$ colortrans 193638
RGB 193638 -> xterm color approx 23 (005f5f)
$ colortrans 232323
RGB 232323 -> xterm color approx 16 (000000)
With a modern urwid, you can then do:
palette = [
('color1', '', '', '', 'h23', 'h23'),
('color2', '', '', '', 'h16', 'h16'),]
I want to send a python list to my ladon service.
Consider the following python list
lst_col_title = [(1, 'Column1', 10, 'L'),(2, 'Column2', 15, 'L'),(3, 'Column3', 15, 'L'),(4, 'Column4', 10, 'L'),(5, 'Column5', 10, 'L')]
Is their any possible way to send this kind of list to ladon services using suds.
EDIT
Actually I want to send the following python variables to a ladon service using suds
str_report_name = 'OPENING BALANCE REPORT'
str_report_format = 'GENERAL'
lst_main_heading = [('<w><h><b><ds>NAME TRAVEL & TOURISM</ds></b></h></w>', 1), ('<p5><b> <b></p5>', 2), ('<b><p2>P.O BOX 3000, JEDDAH 12345, KSA, Phone: 02 6845455</p2></b>', 3), ('<b><p2>Email: info#nametravel.com, Fax: 02 6873455, C.R.No: </p2></b>', 4), ('', 5)]
lst_header = []
lst_report_header = [['', 'CREDIT NOTE', '', '<u><b><w>'], ['', '', '', ''], ['', 'No: RF/1', '', '<b>'], ['To, CASH KAAU (942)', '', 'Date: 01/01/2011', '<b>'], [' P.O. Box No. 3263,DOHA,QATAR', '', 'Code: C022 ', '<b>'], [' Tel: +91 9945 4561 45, Fax: +21 7894 7894', '', '', '<b>'], [' E-Mail: cashkaau123#gmail.com', '', '', '<b>'], ['', '', '', ''], ['Please note that we have CREDITED your account with following details.', '', '', '']]
lst_page_header = []
lst_footer = []
lst_page_footer = []
lst_report_footer = [['Two Thousand Two Hundred Seventeen Saudi Riyal Only ', '', '2217.00', '<b>'], ['', '', '', ''], ['Accountant', 'Created By:ADMIN', 'Manager', ''], ['', '', '', ''], ['Signature', '', '', '']]
lst_report_data = [('', '', '', '', ''), (1, '065-9778821549', 'ABOUNASEF/SEHAM KAMEL MRS', 'JED/CAI/JED', '2584.00'), ('', '', '<i>Less</i>: Cancellation Fee', '', '367.00'), ('', '', '', '', ''), ('', 'THIS IS TO CHECK THE NARRATION PRINTING THIS IS TO CHECK THE NARRATION PRINTING THIS IS TO CHECK THE NARR<i>', '', '', '')]
bln_show_column_heading = True
lst_col_title = [(1, 'Column1', 10, 'L'),(2, 'Column2', 15, 'L'),(3, 'Column3', 15, 'L'),(4, 'Column4', 10, 'L'),(5, 'Column5', 10, 'L')]
This is my ladon service
#ladonize(str,str,[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING], rtype=str)
def generate_pdf_print(self, str_report_name,str_report_format,lst_main_heading, lst_header, lst_report_header, lst_page_header, lst_footer, lst_report_footer, lst_page_footer, lst_report_data, bln_show_column_heading, lst_col_title, **args):
But [PORTABLE_STRING] will not do what I want.
As I am new to web services, I have no idea how to deal with thease type of complex python types.
UPDATE
I have created a new ladon type for
lst_col_title = [(1, 'Column1', 10, 'L'),(2, 'Column2', 15, 'L'),(3, 'Column3', 15, 'L'),(4, 'Column4', 10, 'L'),(5, 'Column5', 10, 'L')]
as:
class Table(LadonType):
slno = int
col_title = PORTABLE_STRING
col_size = int
col_align = PORTABLE_STRING
and modified the #ladonize as,
#ladonize(str,str,[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[PORTABLE_STRING],[ Table ], rtype=str)
Is this a right way?
It raises an error for me
' Server raised fault: '
classname: <class 'AccountEngine.Table'>
Dictionary expected for prime_dict got "<type 'unicode'>" of value "1"'
I believe this is what you are looking for. Essentially what you want to do is convert each of your tuples to a LadonType, then return a list of those types, similar to the listAlbums and listBands methods in that tutorial. Here's another tutorial on LadonTypes.
Ladon
from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType
class Calculator(object):
class Table(LadonType):
slno = int
colTitle = str
colSize = int
colAlign = str
#ladonize([Table],rtype=int) #notice the [], around table that means the input will be a list of Table LadonTypes.
def setTables(self,tables):
return len(tables)
Suds
from suds.client import Client
client = Client('http://localhost:8888/Calculator/soap/description')
table = client.factory.create('Table')
table.slno = 1
table.colTitle = 'col1'
table.colSize = 10
table.colAlign = 'L'
table2 = client.factory.create('Table')
table2.slno = 2
table2.colTitle = 'col2'
table2.colSize = 15
table2.colAlign = 'L'
tableList = [table, table2]
print client.service.setTables(tableList)
I have solved this problem, by converting each list as string.
self.generate_pdf_file(str_report_name,
str_report_format,
str(lst_main_heading),
str(lst_header),
str(lst_report_header),
str(lst_page_header),
str(lst_footer),
str(lst_report_footer),
str(lst_page_footer),
str(lst_report_data),
bln_show_column_heading,
str(lst_col_title))
Now my #ladonize looks like:
#ladonize(str,str,str,str,str,str,str,str,str,str,str,str, rtype=str)
def generate_pdf_print(self, str_report_name,str_report_format,lst_main_heading, lst_header, lst_report_header, lst_page_header, lst_footer, lst_report_footer, lst_page_footer, lst_report_data, bln_show_column_heading, lst_col_title, **args):
And reverted those valuesusing eval as follows:
def generate_pdf_print(self,db,
str_report_name = 'OPENING BALANCE REPORT',
str_report_format = 'GENERAL',
lst_main_heading = [],
lst_header = [],
lst_report_header = [],
lst_page_header = [],
lst_footer = [],
lst_report_footer = [],
lst_page_footer = [],
lst_report_data = [],
bln_show_column_heading = True,
lst_col_title = [],
int_count_blocks_of_data_in_print = 1,
str_pdf_theme = 'Default'
):
lst_main_heading = eval(lst_main_heading)
lst_header = eval(lst_header)
lst_report_header = eval(lst_report_header)
lst_page_header = eval(lst_page_header)
lst_footer = eval(lst_footer)
lst_page_footer = eval(lst_page_footer)
lst_report_footer = eval(lst_report_footer)
lst_report_data = eval(lst_report_data)
bln_show_column_heading = True
lst_col_title = eval(lst_col_title)
It´s posiible send a list of LadonType objects.
Example:
class Mov(LadonType):
id = int
text = str
#ladonize([Mov], rtype=PORTABLE_STRING)
def ReceiveMovs(self, moves):
....
Client (ArrayOfxxx, where xxx is the object class name):
moves = client.factory.create("ArrayOfMov")
data1 = client.factory.create("Mov")
data1.id = 1
data1.text = "Test 1"
data2 = client.factory.create("Mov")
data2.id = 2
data2.text = "Test 2"
and the most important part in client:
moves["item"] = [data1, data2]
res = client.service.ReceiveMovs(moves=moves)
I hope this help!!
Apologies for my english.