how to create the special table to wrap my data in python? - python

Here is my data.
['date', 'x1', 'x2', 'x3', 'x4', 'x5']
['2014-02-26', '302964.13', '11715.30', '1389.87', '404.63', '304354.00']
['2014-02-25', '297672.69', '7406.57', '1733.45', '345.70', '299406.14']
['2014-02-24', '297957.19', '9933.49', '1514.91', '345.56', '299472.10']
['2014-02-21', '312826.51', '9365.13', '1308.66', '266.03', '314135.17']
['2014-02-20', '325884.05', '10152.87', '1576.65', '483.17', '327460.70']
['2014-02-19', '330013.56', '17205.76', '1729.29', '709.73', '331742.85']
['2014-02-18', '327645.32', '11282.95', '1359.65', '190.63', '329004.96']
['2014-02-17', '324224.52', '13298.12', '1080.77', '145.35', '325305.29']
['2014-02-14', '324022.62', '11353.54', '1254.47', '183.25', '325277.08']
['2014-02-13', '330918.46', '25570.06', '1597.78', '695.83', '332516.24']
['2014-02-12', '324835.65', '10588.67', '1630.50', '456.97', '326466.15']
['2014-02-11', '325785.09', '23822.77', '2214.77', '599.14', '327999.86']
['2014-02-10', '319886.55', '12657.06', '1759.45', '169.27', '321646.00']
['2014-02-07', '312989.44', '11284.82', '2225.87', '165.09', '315215.31']
['2014-01-30', '305685.92', '3278.42', '1441.53', '65.08', '307127.45']
['2014-01-29', '310027.36', '4866.76', '1211.53', '171.60', '311238.89']
['2014-01-28', '311252.25', '5307.47', '1627.93', '110.17', '312880.19']
['2014-01-27', '313464.93', '5316.03', '1579.17', '170.52', '315044.10']
['2014-01-24', '315828.12', '9967.38', '1614.39', '222.52', '317442.51']
I want to wrap it into the form.
I can create the head and body
Head:
| date | x1 | x2 | x3 | x4 | x5 |
body:
|2014-02-27| 303664.88| 9753.48| 1455.21| 483.72| 305120.10|
|2014-02-26| 302964.13| 11715.30| 1389.87| 404.63| 304354.00|
|2014-02-25| 297672.69| 7406.57| 1733.45| 345.70| 299406.14|
|2014-02-24| 297957.19| 9933.49| 1514.91| 345.56| 299472.10|
|2014-02-21| 312826.51| 9365.13| 1308.66| 266.03| 314135.17|
|2014-02-20| 325884.05| 10152.87| 1576.65| 483.17| 327460.70|
|2014-02-19| 330013.56| 17205.76| 1729.29| 709.73| 331742.85|
|2014-02-18| 327645.32| 11282.95| 1359.65| 190.63| 329004.96|
|2014-02-17| 324224.52| 13298.12| 1080.77| 145.35| 325305.29|
|2014-02-14| 324022.62| 11353.54| 1254.47| 183.25| 325277.08|
|2014-02-13| 330918.46| 25570.06| 1597.78| 695.83| 332516.24|
|2014-02-12| 324835.65| 10588.67| 1630.50| 456.97| 326466.15|
|2014-02-11| 325785.09| 23822.77| 2214.77| 599.14| 327999.86|
|2014-02-10| 319886.55| 12657.06| 1759.45| 169.27| 321646.00|
|2014-02-07| 312989.44| 11284.82| 2225.87| 165.09| 315215.31|
|2014-01-30| 305685.92| 3278.42| 1441.53| 65.08| 307127.45|
|2014-01-29| 310027.36| 4866.76| 1211.53| 171.60| 311238.89|
|2014-01-28| 311252.25| 5307.47| 1627.93| 110.17| 312880.19|
|2014-01-27| 313464.93| 5316.03| 1579.17| 170.52| 315044.10|
|2014-01-24| 315828.12| 9967.38| 1614.39| 222.52| 317442.51|
how to create the first line in the table?
┌─────┬─────┬─────┬────┬─────┬──────┐
and the third line in the table?
├─────┼─────┼─────┼────┼─────┼──────┤
and the last line in the table?
└─────┴─────┴─────┴────┴─────┴──────┘
please write it in python.
the form is :
+------------+---------+-------------+
| column_one | col_two | column_3 |
+------------+---------+-------------+
| 0 | 0.0001 | ABCD |
| 1 | 1e-005 | ABCD |
| 2 | 1e-006 | long string |
| 3 | 1e-007 | ABCD |
+------------+---------+-------------+
not absolutely the same as mine posted here.

How obout taking the prettytable output (suggested in comment to your post) and just replacing the + chars with ones from your example? Pretty simple thing.

Related

Using Pyspark how to convert plain text to csv file

When I created a hive table, the data is as follows.
data file
<__name__>abc
<__code__>1
<__value__>1234
<__name__>abcdef
<__code__>2
<__value__>12345
<__name__>abcdef
<__code__>2
<__value__>12345
1234156321
<__name__>abcdef
<__code__>2
<__value__>12345
...
Can I create a table right away without converting the file?
It's a plain text file, three columns are repeated.
How to convert dataframe? or csv file?
I want
| name | code | value
| abc | 1 | 1234
| abcdef | 2 | 12345
...
or
abc,1,1234
abcdef,2,12345
...
I solved my problem like this.
data = spark.read.text(path)
rows = data.rdd.zipWithIndex().map(lambda x: Row(x[0].value, int(x[1]/3)))
schema = StructType() \
.add("col1",StringType(), False) \
.add("record_pos",IntegerType(), False)
df = spark.createDataFrame(rows, schema)
df1 = df.withColumn("key", regexp_replace(split(df["col1"], '__>')[0], '<|__', '')) \
.withColumn("value", regexp_replace(regexp_replace(split(df["col1"], '__>')[1], '\n', '<NL>'), '\t', '<TAB>'))
dataframe = df1.groupBy("record_pos").pivot("key").agg(first("value")).drop("record_pos")
dataframe.show()
val path = "file:///C:/stackqustions/data/stackq5.csv"
val data = sc.textFile(path)
import spark.implicits._
val rdd = data.zipWithIndex.map {
case (records, index) => Row(records, index / 3)
}
val schema = new StructType().add("col1", StringType, false).add("record_pos", LongType, false)
val df = spark.createDataFrame(rdd, schema)
val df1 = df
.withColumn("key", regexp_replace(split($"col1", ">")(0), "<|__", ""))
.withColumn("value", split($"col1", ">")(1)).drop("col1")
df1.groupBy("record_pos").pivot("key").agg(first($"value")).drop("record_pos").show
result:
+----+------+-----+
|code| name|value|
+----+------+-----+
| 1| abc| 1234|
| 2|abcdef|12345|
| 2|abcdef|12345|
| 2|abcdef|12345|
+----+------+-----+

How to generate report(xlsx format) and Qweb Report in odoo 12

I'm working with account-financial-reporting module (OCA).
I want to add tree structure according to Code :
if account.code = "xxx000" or "yyy000" , then change the line style to bold ,
and get the other codes in ascending order . Example:
Code | |
-------------------------
xxx000 | |
-------------------------
|xxx001 |
-------------------------
|xxx003 |
-------------------------
yyy000 | |
-------------------------
|yyy002 |
-------------------------
|yyy005 |
-------------------------
def _generate_report_content(self, workbook, report):
if not report.show_partner_details:
# Display array header for account lines
self.write_array_header()
# For each account
for account in report.account_ids.filtered(lambda a: not a.hide_line):
if not report.show_partner_details:
# Display account lines
self.write_line(account, 'account')
Any help please ?

How to use Fl_Tabs in pyFltk

I am trying to make a card game programs using Fl_Tabs or Fl_Wizard but can't find any instructions on how to do so in pyFltk. Is there an example or any documentation showing the basic code of creating the Fl_Tabs because I couldn't find anything on the Internet.
from fltk import *
x=Fl_Tabs(20,20,500,500)
x.show()
Fl.run()
When I run the above program that, nothing will show.
Thanks in advance.
Example of how to use Fl_Tabs. This is a python version of the one from Erco's cheat page
#!/usr/bin/python
# python version of http://seriss.com/people/erco/fltk/#Fl_Tabs
# _____ _____
# __/ Aaa \/ Bbb \______________________
# | _______ |
# | |_______| |
# | _______ |
# | |_______| |
# | _______ |
# | |_______| |
# |______________________________________|
#
from fltk import *
def GroupAAA():
aaa = Fl_Group(10,35,500-20,200-45,"Aaa")
b1 = Fl_Button(50, 60,90,25,"Button A1"); b1.color(88+1)
b2 = Fl_Button(50, 90,90,25,"Button A2"); b2.color(88+2)
b3 = Fl_Button(50,120,90,25,"Button A3"); b3.color(88+3)
aaa.end()
def GroupBBB():
bbb = Fl_Group(10,35,500-10,200-35,"Bbb")
b1 = Fl_Button( 50,60,90,25,"Button B1"); b1.color(88+1)
b2 = Fl_Button(150,60,90,25,"Button B2"); b2.color(88+3)
b3 = Fl_Button(250,60,90,25,"Button B3"); b3.color(88+5)
b4 = Fl_Button( 50,90,90,25,"Button B4"); b4.color(88+2)
b5 = Fl_Button(150,90,90,25,"Button B5"); b5.color(88+4)
b6 = Fl_Button(250,90,90,25,"Button B6"); b6.color(88+6)
bbb.end()
win = Fl_Window(500,200,"Tabs Example")
tabs = Fl_Tabs(10,10,500-20,200-20)
GroupAAA()
GroupBBB()
tabs.end()
win.end()
win.show()
Fl.run()

Search and Find through a list Python

I have a main text file that looks like this:
STATUS| CRN| SUBJECT| SECT| COURSE| CREDIT| INSTR.| BLDG/RM| DAY/TIME| FROM / TO|
OPEN| 43565| ACA6202| 10| Acting II| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43566| ACA6206| 10| Topics:Classical Drama/Cult II| 2.00| Jacobson, L| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43567| ACA6210| 10| Text II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43568| ACA6212| 10| Voice and Speech II| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43569| ACA6216| 10| Movement II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43570| ACA6220| 10| Alexander Technique II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43571| ACA6224| 10| Stage Combat II| 2.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 43572| ACA6228| 10| Practicum IV| 3.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
OPEN| 44500| ACA6595| 10| Selected Topics| 1.00| Logan, G| SEE DEPT| | 01/12/15 - 04/27/15|
My code below gathers only the "SUBJECT" column and strips the numbers from the string. So for example, the output from the top of the file would print several "ACA"s.
with open ("/Users/it/Desktop/Classbook/classAbrevs.txt", "r") as myfile:
subsAndAbrevsMap = tuple(open("/Users/it/Desktop/Classbook/classAbrevs.txt", 'r'))
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
masterSchedule = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))
for masterline in masterSchedule:
masterline.strip()
masterSplitLine = masterline.split("|")
if masterSplitLine[0] != "STATUS":
subjectAbrev = ''.join([i for i in masterSplitLine[2] if not i.isdigit()])
I have another .txt file that looks like this:
Academy for Classical Acting,ACA
Accountancy,ACCY
Africana Studies,AFST
American Studies,AMST
Anatomy & Regenerative Biology,ANAT
Anthropology,ANTH
Applied Science,APSC
Arabic,ARAB
Art/Art History,AH
Art/Fine Arts,FA
Astronomy,ASTR
Biochemistry,BIOC
Biological Sciences,BISC
In my code below, I check to see if the abbreviations(column 2) in my second .txt equal the abbreviations generated from my first .txt document. If it is a match I would like to append the full class name:
#open 2nd .txt, strip and split
for subsline in subsAndAbrevsMap:
subsline.strip()
subLineSplit = subsline.split(",")
print "subLineSplit is: " + subsline[0]
if subLineSplit[1] == subjectAbrev:
realSubjectName = subLineSplit[0]
print "The subject name for abrev " + subjectAbrev + " is " + realSubjectName
I want the output to print:
"The subject name for abrev ACA is Academy for Classical Acting"
What am I doing wrong?
First of all, these are csv files, so use your csv module!
# path to first file is ~/classes.csv
# path to second file is ~/abbr.csv
import csv
with open("~/classes.csv", 'rU') as classes_csv,\
open("~/abbr.csv", 'rU') as abbr_csv:
classes = csv.reader(classes_csv, delimiter='|')
abbr = csv.reader(abbr_csv, delimiter=',')
header = next(classes)
abbr_dict = {line[1].strip():line[0].strip() for line in abbr}
# create a lookup dictionary for your tags -> names
class_tags = (line[2].strip("0123456789 ") for line in classes)
# create a genexp for all the extant tags in ~/classes.csv
result = {tag:abbr_dict[tag] for tag in class_tags if tag in abbr_dict}
Then it should be easy to format your result.
for abbr,cls in result.items():
print("The abbreviation for {} is {}".format(cls,abbr))

PyGTK Spacing in an HBox

I'm new to GTK, I'm trying to figure out how to accomplish something like this:
+---+------+---+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
+---+------+---+
I want this done in an HBox. How would I accomplish this? Thanks.
It is done with "packing".
I always keep the class reference under my pillow : http://www.pygtk.org/docs/pygtk/gtk-class-reference.html
Samples in the good tutorial found here :
http://www.pygtk.org/pygtk2tutorial/sec-DetailsOfBoxes.html
And finally, this shows up something like your drawing :
import gtk as g
win = g.Window ()
win.set_default_size(600, 400)
win.set_position(g.WIN_POS_CENTER)
win.connect ('delete_event', g.main_quit)
hBox = g.HBox()
win.add (hBox)
f1 = g.Frame()
f2 = g.Frame()
f3 = g.Frame()
hBox.pack_start(f1)
hBox.pack_start(f2)
hBox.pack_start(f3)
win.show_all ()
g.main ()
Have fun ! (and I hope my answer is helpful)
The answer is pack_start() and pack_end()
The function has a few parameters you can send to it that give you the desired effect
If you use Louis' example:
hBox.pack_start(f1, expand =False, fill=False)
hBox.pack_start( f2, expand=True, fill=True, padding=50)
hBox.pack_end(f3, expand=False, fill=False)
Hope that helps!

Categories

Resources