output formatting error getting extra dots [duplicate] - c++

This question already has answers here:
Which iomanip manipulators are 'sticky'?
(3 answers)
Restore the state of std::cout after manipulating it
(9 answers)
C++ - How to reset the output stream manipulator flags [duplicate]
(5 answers)
Closed 5 months ago.
my output formatting is wrong, here is display function below:
std::ostream &Seat::display(std::ostream &coutRef) const
{
if(isEmpty()){
coutRef<<"Invalid Seat!";
}
//check if seat no is valid or not
else if(!validate(m_row,m_letter)){
char temp[41];
strncpy(temp,m_passengerName,40);
temp[40]='\0';
coutRef<<temp<<setw(41-strlen(temp))<<setfill('.')<<" "<<"___";
}
else{
char temp[41];
strncpy(temp,m_passengerName,40);
temp[40]='\0';
coutRef<<temp<<setw(41-strlen(temp))<<setfill('.')<<" "<<m_row<<m_letter;
}
return coutRef;
}
My output Below:
..1- Business Class Window: Baby Gerald............................. 1A
..2- Business Class Aisle: Groundskeeper Willie.................... 1B
..3- Business Class Aisle: Dolph Starbeam.......................... 1E
..4- Business Class Window: Kirk Van Houten......................... 1F
..5- Business Class Window: Artie Ziff.............................. 2A
..6- Business Class Aisle: Edna Krabappel.......................... 2B
..7- Business Class Aisle: Luann Van Houten........................ 2E
..8- Business Class Window: Janey Powell............................ 2F
..9- Business Class Window: Akira Kurosawa.......................... 3A
.10- Business Class Aisle: Luigi Risotto........................... 3B
.11- Business Class Aisle: Homer Simpson........................... 3E
.12- Business Class Window: Selma Bouvier........................... 3F
.13- Business Class Window: Wendell Borton.......................... 4A
.14- Business Class Middle: Manjula Nahasapeemapetilon.............. 4B
.15- Business Class Middle: Kearney Zzyzwicz........................ 4E
.16- Business Class Window: Brandine Spuckler....................... 4F
.17- Invalid seat assigned: Moe Szyslak............................. ___
.18- Invalid seat assigned: Ralph Wiggum............................ ___
.19- Economy Plus Aisle: Barney Gumble........................... 7D
I just want to remove dots from the front of the number like ..1, ..2, ..3 I just want to remove .. from the front. I just want to print like this 1,2,3

Related

How To Interpret Least Square Means and Standard Error

I am trying to understand the results I got for a fake dataset. I have two independent variables, hours, type and response pain.
First question: How was 82.46721 calculated as the lsmeans for the first type?
Second question: Why is the standard error exactly the same (8.24003) for both types?
Third question: Why is the degrees of freedom 3 for both types?
data = data.frame(
type = c("A", "A", "A", "B", "B", "B"),
hours = c(60,72,61, 54,68,66),
# pain = c(85,95,69, 73, 29, 30)
pain = c(85,95,69, 85,95,69)
)
model = lm(pain ~ hours + type, data = data)
lsmeans(model, c("type", "hours"))
> data
type hours pain
1 A 60 85
2 A 72 95
3 A 61 69
4 B 54 85
5 B 68 95
6 B 66 69
> lsmeans(model, c("type", "hours"))
type hours lsmean SE df lower.CL upper.CL
A 63.5 82.46721 8.24003 3 56.24376 108.6907
B 63.5 83.53279 8.24003 3 57.30933 109.7562
Try this:
newdat <- data.frame(type = c("A", "B"), hours = c(63.5, 63.5))
predict(model, newdata = newdat)
An important thing to note here is that your model has hours as a continuous predictor, not a factor.

subset of data using regular exp in spark

I have sample data like below
class: 9
section: A
stud : Robert
subject: maths
mark : 69
subject:science
mark: 75
stud : Billy
subject: maths
mark : 69
subject:science
mark: 75
stud : Venice
subject: maths
mark : 69
subject:science
mark: 75
stud : Marc
subject: maths
mark : 69
subject:science
mark: 75
class: 10
section: A
stud : Agnes
subject: maths
mark : 69
subject:science
mark: 75
stud : Sarah
subject: maths
mark : 69
subject:science
mark: 75
stud : Scott
subject: maths
mark : 69
subject:science
mark: 75
stud : Alex
subject: maths
mark : 69
subject:science
mark: 75
line1
line2
line3
...
line n
I am trying to extract class 9 student data out of this file. Here is my code
val datafile = sc.textFile("file.txt").collect().mkString(" ")
// to take the data I needed from whole file
val datpattern = """(class: 9).*?(?=\bline\s)
val finaldata = datpattern.findAllIn(datafile)
//student data extract regex
val stupattern = "section: (\S+)\s+ stud : ([\w\S]+)\s+ subject: ([\w\S]+)\s+ mark : (\d+)"""".r
val finalresult = finaldata.flatMap { a => stupattern findAllIn a }
.map {l =
val stupattern(section,stuname,sub,mark) = l
(section,stuname,sub,mark)
}
.foreach(println)
But this gave me only first record in each class that too only the first subject & mark. (Robert maths mark & Agnes Maths mark from class 9 & 10th S A section.
I thought this is because only that matches entire pattern.
I tried to change it like with 0 or more occurences for subject & mark. something like below (Only the lines I have changed given below)
val stupattern = "section: (\S+)\s+ stud : ([\w\S]+)\s+ (subject: ([\w\S]+)\s+ mark : (\d+))*"""".r
val finalresult = finaldata.flatMap { a => stupattern findAllIn a }
.map {l =
val stupattern(section,stuname,{sub,mark}) = l//This doesn't even let me compile
(section,stuname,sub,mark)//This doesn't even let me compiled
}
.foreach(println)
It error out like for those 2 lines "Illegal start of pattern".
Can someone tell me how to extract repeat subset of data from above?
Thanks in Advance.

match elements from two files, how to write the intended format to a new file

I am trying to update my text file by matching the first column to another updated file's first column, after match it, it will update the old file.
Here is my oldfile:
Name Chr Pos ind1 in2 in3 ind4
foot 1 5 aa bb cc
ford 3 9 bb cc 00
fake 3 13 dd ee ff
fool 1 5 ee ff gg
fork 1 3 ff gg ee
Here is the newfile:
Name Chr Pos
foot 1 5
fool 2 5
fork 2 6
ford 3 9
fake 3 13
The updated file will be like:
Name Chr Pos ind1 in2 in3 ind4
foot 1 5 aa bb cc
fool 2 5 ee ff gg
fork 2 6 ff gg ee
ford 3 9 bb cc 00
fake 3 13 dd ee ff
Here is my code:
#!/usr/bin/env python
import sys
inputfile_1 = sys.argv[1]
inputfile_2 = sys.argv[2]
outputfile = sys.argv[3]
inputfile1 = open(inputfile_1, 'r')
inputfile2 = open(inputfile_2, 'r')
outputfile = open(outputfile, 'w')
ind = inputfile1.readlines()
cm = inputfile2.readlines()[1:]
outputfile.write(ind[0]) #add header
for i in ind:
i = i.split()
for j in cm:
j = j.split()
if j[0] == i[0]:
outputfile.writelines(j[0:3] + i[3:])
outputfile.write('\n')
inputfile1.close()
inputfile2.close()
outputfile.close()
When I ran it, ./compare_substitute_2files.py oldfile newfile output
the values were updated for the file, but they did not follow the order of the new file, and no space was there as indicated in the output below.
Name Chr Pos ind1 in2 in3 ind4
foot15aabbcc
ford39bbcc00
fake313ddeeff
fool25eeffgg
fork26ffggee
My question is how to match to the exact order and give spaces to each element in the list when write them out? Thanks!
file.write accepts string as its parameter.
If you want write sequences of strings instead of string, use file.writelines method instead:
outputfile.writelines(j[0:2] + i[3:])

How to read Fortran fixed-width formatted text file in Python?

I have a Fortran formatted text file (here is 3 first rows):
00033+3251 A B C? 6.96 5.480" 358 9.12 F0V 0.00 2.28s 1.00: 2MASS, dJ=1.3
00033+3251 Aa Ab Aab S1,E 0.62 0.273m 0 9.28 F0V 11.28 K2 1.68* 0.32* SB 1469
00033+3251 Aab Ac A E* 4.26 0.076" 0 9.12 F0V 0.00 2.00s 0.28* 2008MNRAS.383.1506
and the file format description:
--------------------------------------------------------------------------------
Bytes Format Units Label Explanations
--------------------------------------------------------------------------------
1- 10 A10 --- WDS WDS(J2000)
12- 14 A3 --- Primary Designation of the primary
16- 18 A3 --- Secondary Designation of the secondary component
20- 22 A3 --- Parent Designation of the parent (1)
24- 29 A6 --- Type Observing technique/status (2)
31- 35 F5.2 d logP ? Logarithm (10) of period in days
37- 44 F8.3 --- Sep Separation or axis
45 A1 --- x_Sep ['"m] Units of sep. (',",m)
47- 49 I3 deg PA Position angle
51- 55 F5.2 mag Vmag1 V-magnitude of the primary
57- 61 A5 --- SP1 Spectral type of the primary
63- 67 F5.2 mag Vmag2 V-magnitude of the secondary
69- 73 A5 --- SP2 Spectral type of the secondary
75- 79 F5.2 solMass Mass1 Mass of the primary
80 A1 --- MCode1 Mass estimation code for primary (3)
82- 86 F5.2 solMass Mass2 Mass of the secondary
87 A1 --- MCode2 Mass estimation code for secondary (3)
89-108 A20 --- Rem Remark
How to read my file in Python. I have found only read_fwf function from the pandas library.
import pandas as pd
filename = 'systems'
columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),(36,44),(45,45),(46,49),(50,55),(56,61),(62,67),(68,73),(74,79),(80,80),(81,86),(87,87),(88,108))
data = pd.read_fwf(filename, colspecs = columns, header=None)
Is this the only possible and effective way? I hope I can do this without pandas. Have you any suggestions?
columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),
(36,44),(44,45),(46,49),(50,55),(56,61),(62,67),
(68,73),(74,79),(79,80),(81,86),(86,87),(88,108))
string=file.readline()
dataline = [ string[c[0]:c[1]] for c in columns ]
note the column indices are (startbyte-1,endbyte) so that a single character field is
eg: (44,45)
this leaves you with a list of strings. You probably want to do conversion to floats, integers, etc. There are a number of questions here on that topic..
There is a module FortranRecordReader but it is weak with the stars, comments, etc that modern fortran files contain. Still, for a nice file, it is useful, in combination with namedtuple. Example:
from fortranformat import FortranRecordReader
fline=FortranRecordReader('(a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5)')
from collections import namedtuple
record=namedtuple('nucleo','cc NZ N Z A el o massexcess uncmassex binding uncbind B beta uncbeta am_int am_float uncatmass')
f=open('AME2012.mas12.ff','r')
for line in f:
nucl=record._make(fline.read(line))
You can try also the module "parse", or write yours
This type of file can be read with astropy tables. The header you show looks a lot like a CDS-formatted ascii table, which has a specific reader implemented for it:
http://astropy.readthedocs.org/en/latest/api/astropy.io.ascii.Cds.html#astropy.io.ascii.Cds
Expanding on arivero's answer, you could use fortranformat from pypi - here is what I would try ...
from fortranformat import FortranRecordReader
fmt = FortranRecordReader('(A10,A3,A3,A3,A6,F5.2,F8.3,A1,I3,F5.2,A5,F5.2,A5,F5.2,A1,F5.2,A1,A20)')
with fh as open('myfile.txt', 'r'):
for line in fh:
line_vals = fmt.read(line)
This should convert the values appropriately to numbers, bool etc.

Does order of Tag matters in BER decoding using pyasn1

case :- I need to decode a BER encoded data using pyasn1 , but the order of tag may vary some times. So while decoding the data it rises error. Is the order of Tag really matters even though it is optional Tag's?
Note:- (case1)8 different Tag's in one CDR is in order: AF 30 81 82 83 84 85 86
(case2)Another 8 Tags is in order : AF 30 81 83 84 85 86 82
But i need to decode both the order with single decoding structure , is that possible ?
Here am giving my structure for reference ....
class ChangeOfCharCondition(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('Requested', univ.OctetString().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
namedtype.OptionalNamedType('Negotiated', univ.OctetString().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),
namedtype.OptionalNamedType('Uplink', univ.Integer().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
namedtype.OptionalNamedType('Downlink', univ.Integer().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))),
namedtype.NamedType('changeCondition', univ.Integer().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 5))),
namedtype.NamedType('changeTime', univ.OctetString().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 6)))
)
class TrafficVolumes(univ.SequenceOf):
tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,
tag.tagFormatSimple, 15))
componentType = ChangeOfCharCondition()
class Cdr(univ.Set):
tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,
tag.tagFormatSimple, 20))
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('TrafficVolumes', TrafficVolumes().subtype(
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 15))))
Sample Data(case1)- bytearray('\xaf200\x81\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x82\x0cx01\x12Q\x1fs\x96HHv\xfa\xff\xff\x83\x01\x00\x84\x01\x00\x85\x01\x02\x86\t\x12\x12\x10\x12\x00P+\x050') and it works fine
Sample Data(case2) - bytearray('\xaf200\x81\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x83\x01\x00\x84\x01\x00\x85\x01\x02\x86\t\x12\x12\x10\x12\x00P+\x050\x82\x0cx01\x12Q\x1fs\x96HHv\xfa\xff\xff') in this case it raise error like ,
PyAsn1Error : TagSet(Tag(tagClass=128, tagFormat=0, tagId=2)) not in asn1Spec: None'