exercise about Titanic (from kaggle) : print doesn't display - methods

Can you explain me why I don't have any output ?
Thank you
Python :
name = ['Braund, Mr. Owen Harris',"Panula, Mr. Ernesti Arvid"]
string1 = ["Panula, Mr. Ernesti Arvid"]
if string1 in name:
string1.replace("Panula, Mr. Ernesti Arvid", "Tanula, Mr.Ernesto Arvid")
print(string1)
I tried to display the print function but nothing happens

Related

ValueError: not enough values to pack (expected 2, got 1) syntax error

Hello I am taking an example from learn python the hard way and working to understand python. I have already caught one error in the book and realized you need () to print out strings. So maybe there could be some more mistakes in the syntax. when I ran this file I received an error that said
ValueError: not enough values to pack (expected 2, got 1) syntax error. And another general syntax error. So I can't set prompt to raw_input.
rom sys import argv
script, user_name = argv
prompt = '>'
print ("Hi %s, I'm the $s script.") % user_name, script
print ("I'd like to ask you a few questions.")
print ("Do you like %s?") % user_name
likes = raw_input(prompt)
print ("Where do you live %s") % user_name
lives = raw_input(prompt)
print """
(Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice)
"""& (likes, lives, computer)
Is there a typo in the code example? $s instead of %s in the format string?
I suspect that your actual code has this:
print ("Hi %s, I'm the %s script.") % user_name, script
And you meant to write this:
print "Hi %s, I'm the %s script." % (user_name, script)
That is, print a single string which is the result of applying the format string to two arguments.

Linux shell script, search by an input string

So, there is a large file where I have to conduct several search using bash shell scripting.
The file is like this:
TITLE and AUTHOR ETEXT NO.
Aspects of plant life; with special reference to the British flora,      56900
by Robert Lloyd Praeger
The Vicar of Morwenstow, by Sabine Baring-Gould 56899
[Subtitle: Being a Life of Robert Stephen Hawker, M.A.]
Raamatun tutkisteluja IV, mennessä Charles T. Russell 56898
[Subtitle: Harmagedonin taistelu]
[Language: Finnish]
Raamatun tutkisteluja III, mennessä Charles T. Russell 56897
[Subtitle: Tulkoon valtakuntasi]
[Language: Finnish]
Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Nancy Brandon's Mystery, by Lillian Garis 56894
Nervous Ills, by Boris Sidis 56893
[Subtitle: Their Cause and Cure]
Pensées sans langage, par Francis Picabia 56892
[Language: French]
Helon's Pilgrimage to Jerusalem, Volume 2 of 2, by Frederick Strauss 56891
[Subtitle: A picture of Judaism, in the century
which preceded the advent of our Savior]
Fra Tommaso Campanella, Vol. 1, di Luigi Amabile 56890
[Subtitle: la sua congiura, i suoi processi e la sua pazzia]
[Language: Italian]
The Blue Star, by Fletcher Pratt 56889
Importanza e risultati degli incrociamenti in avicoltura, 56888
di Teodoro Pascal
[Language: Italian]
The Junior Classics, Volume 3: Tales from Greece and Rome, by Various 56887
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
TITLE and AUTHOR ETEXT NO.
The American Missionary, Volume 41, No. 1, January, 1887, by Various 56886
Morganin miljoonat, mennessä Sven Elvestad 56885
[Author a.k.a. Stein Riverton]
[Subtitle: Salapoliisiromaani]
[Language: Finnish]
"Trip to the Sunny South" in March, 1885, by L. S. D 56884
Balaam and His Master, by Joel Chandler Harris 56883
[Subtitle: and Other Sketches and Stories]
Susien saaliina, mennessä Jack London 56882
[Language: Finnish]
Forged Egyptian Antiquities, by T. G. Wakeling 56881
The Secret Doctrine, Vol. 3 of 4, by Helena Petrovna Blavatsky 56880
[Subtitle: Third Edition]
No Posting 56879
First love and other stories, by Iván Turgénieff                         56878
Now I have to search it with etext no, author name and title..
Like If I search by an etext no: like etext 56900:
It should return
Aspects of plant life; with special reference to the British flora, 56900
Well I am new to shell scripting. And I can only read the file.
With this:
#!/bin/sh
read -p 'string to search ' searchstring
grep --color searchstring GUTINDEX.ALL | #condition
I don't know what kind of condition I should use to search by author name or etext no....
As others have already pointed out, the use of grep alone is not how you would really approach this. A rather substantial improvement could be accomplished by using Awk instead of grep, but for a real production system, you would parse out the fields into a relational database, and use SQL to search instead. With database indexing, searching will then be much much quicker than sequentially scanning the entire index file for each search.
But if you are confined to just grep, here is a quick and dirty attempt.
author () { grep -E "(by|par|di|mennessä) $#" GUTINDEX.ALL; }
index () { grep " $#\$" GUTINDEX.ALL; }
title () { grep "^$#" GUTINDEX.ALL; }
This declares three shell functions which search different parts of the file, by way of supplying an anchor expression (^ matches beginning of line, $ matches end of line) or a suitable context.
Having the search expression as a command-line argument instead of requiring interactive input is generally a huge usability improvement. Now, you can use your shell's history mechanism to recall and possibly edit earlier searches, and build new scripts on top of these simple building blocks.
(By the way, "mennessä" is not at all a correct Finnish localization here. I have reported a bug to Project Gutenberg.)
You could start with something like this, but as #tom-fenech points out, it's rather unreliable in the absence of structured input.
For instance, the author names are not consistently prefixed, they appear sometimes under "Subtitle", and rarely under "Author" tag.
#!/bin/bash
CATALOG=/tmp/s
function usage()
{
echo "Usage:"
echo "$0 [etext <key>] [author <id>]"
exit 1;
}
function process_etext()
{
local searchKey=$1
egrep "${searchKey}" ${CATALOG} | awk -F"${searchKey}" '{print $1}'
}
function process_author()
{
local searchKey=$1
egrep -b1 "${searchKey}" ${CATALOG} | egrep "[[:digit:]]{5}"
}
for key in "$#"
do
key="$1"
case $key in
etext|author)
process_${key} $2
shift; shift;
;;
*)
[ -z ${key} ] || usage
;;
esac
done

Reversing and Splitting in Python

I have a file "names.txt". The contents are
"Smith,RobJones,MikeJane,SallyPetel,Brian"
and I want to read "names.txt" and make a new file "names2.txt" that looks like:
"Rob Smith Mike Jones Sally Jane Brian Petel"
I know I should be using #rstrip(\n) and #.split(',')
So far I have:
namesfile = input('Enter name of file: ') #open names.txt
openfile = open(namesfile, 'r')
This will do exactly that. You might be able to polish this and make it more elegant and I encourage you to do so:
import re
with open('names.txt') as f:
# Split the names
names = re.sub(r'([A-Z])(?![A-Z])',r',\1',f.read()).split(',')
# Filter empty results
names = [n for n in names if n != '']
# Swap pairs with each other
for i in range(len(names)):
if((i+1)%2 == 0):
names[i], names[i-1] = names[i-1], names[i]
print ' '.join(names)

Moving chunks of data in a file with awk

I'm moving my bookmarks from kippt.com to pinboard.in.
I exported my bookmarks from Kippt and for some reason, they were storing tags (preceded by #) and description within the same field. Pinboard keeps tags and description separated.
This is what a Kippt bookmark looks like after export:
<DT>This is a title
<DD>#tag1 #tag2 This is a description
This is what it should look like before importing into Pinboard:
<DT>This is a title
<DD>This is a description
So basically, I need to replace #tag1 #tag2 by TAGS="tag1,tag2" and move it on the first line within <A>.
I've been reading about moving chunks of data here: sed or awk to move one chunk of text betwen first pattern pair into second pair?
I haven't been to come up with a good recipe so far. Any insight?
Edit:
Here's an actual example of what the input file looks like (3 entries out of 3500):
<DT>Phabricator
<DD>#bug #tracking
<DT>The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz
<DT>Icelandic Farm Holidays | Local experts in Iceland vacations
<DD>#iceland #tour #car #drive #self Self-driving tour of Iceland
This might not be the most beautiful solution, but since it seems to be a one-time-thing it should be sufficient.
import re
dt = re.compile('^<DT>')
dd = re.compile('^<DD>')
with open('bookmarks.xml', 'r') as f:
for line in f:
if re.match(dt, line):
current_dt = line.strip()
elif re.match(dd, line):
current_dd = line
tags = [w for w in line[4:].split(' ') if w.startswith('#')]
current_dt = re.sub('(<A[^>]+)>', '\\1 TAGS="' + ','.join([t[1:] for t in tags]) + '">', current_dt)
for t in tags:
current_dd = current_dd.replace(t + ' ', '')
if current_dd.strip() == '<DD>':
current_dd = ""
else:
print current_dt
print current_dd
current_dt = ""
current_dd = ""
print current_dt
print current_dd
If some parts of the code are not clear, just tell me. You can of course use python to write the lines to a file instead of printing them, or even modify the original file.
Edit: Added if-clause so that empty <DD> lines won't show up in the result.
script.awk
BEGIN{FS="#"}
/^<DT>/{
if(d==1) print "<DT>"s # for printing lines with no tags
s=substr($0,5);tags="" # Copying the line after "<DT>". You'll know why
d=1
}
/^<DD>/{
d=0
m=match(s,/>/) # Find the end of the HREF descritor first match of ">"
for(i=2;i<=NF;i++){sub(/ $/,"",$i);tags=tags","$i} # Concatenate tags
td=match(tags,/ /) # Parse for tag description (marked by a preceding space).
if(td==0){ # No description exists
tags=substr(tags,2)
tagdes=""
}
else{ # Description exists
tagdes=substr(tags,td)
tags=substr(tags,2,td-2)
}
print "<DT>" substr(s,1,m-1) ", TAGS=\"" tags "\"" substr(s,m)
print "<DD>" tagdes
}
awk -f script.awk kippt > pinboard
INPUT
<DT>Phabricator
<DD>#bug #tracking
<DT>The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz
<DT>Icelandic Farm Holidays | Local experts in Iceland vacations
<DD>#iceland #tour #car #drive #self Self-driving tour of Iceland
OUTPUT:
<DT>Phabricator
<DD>
<DT>The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz
<DT>Icelandic Farm Holidays | Local experts in Iceland vacations
<DD> Self-driving tour of Iceland

shell script to parse log file

I am given a log file(see below), I need to make it to this format using bash script:
title pdfspool date rip date bmpspool date CLAB date
Sometitle12 10/09/23 00:56:40 10/9/23 0:56:46 10/9/23 0:56:50 10/9/23 1:01:13
log file
!!Begin
Source aserver:pdf_spool:the, Job 844b015e0043469e, Inst 844b015e0043469e
Title Sometitle12.pdf
Action Started Received, Ok Date 10/09/23 00:56:40
For Administrator
(8) DataType = PDF
(17) Source = srv01:aserver:file_input:0
!!End
!!Begin
Source aserver:rip:rip1, Job 844b015e0043469e, Inst 844b015e004346a0
Title Sometitle12.pdf Cyan 1
Action Started Transmit, Ok Date 10/09/23 00:56:46
For Administrator
(8) DataType = Bitmap
(1) Destination = srv01:bserver:bmp_spool:the
(4) Parent = 844b015e0043469e/844b015e0043469e
!!End
!!Begin
Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0
Title Sometitle12.pdf Cyan 1
Action Started Received, Ok Date 10/09/23 00:56:50
For Administrator
(8) DataType = Bitmap
(17) Source = srv01:aserver:rip:rip1
!!End
!!Begin
Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0
Title Sometitle12.pdf Cyan 1
Action Atomic Accepted, Ok Date 10/09/23 01:01:13
For Administrator
(8) DataType = Bitmap
(2) Source Queue = ^03Newspaper ltd(MP)^Date - 24MP^Site - N^
(5) Requested By = clab
(15) Approval Status = Waiting Approved
Changed from Waiting to Approved by clab.
!!End
Ideas welcome.
Thanks!
awk 'BEGIN{}
/Action Started Received/ && !c{ pdfspooldate=$(NF-1)$NF ;c++}
/Action Started Received/ && c{ bmppooldate=$(NF-1)$NF ;c=0}
/Action Started Transmit/{ ripdate=$(NF-1)$NF }
/title/ { title=$2}
/Action Atomic Accepted/{ clabdate=$(NF-1)$NF }
END{ print title,pdfspooldate,ripdate,clabdate }' file
Use awk. Write a state machine. Switch states when you see /^!!Begin$/, record your data, and dump your output and switch back when you see /^!!End$/.
If you use Perl/Python/Ruby, you should able to use regular expression matching in one line (the matching part). Use the multiline mode where a . will match the newline character. I think awk or sed should be able to use regular expression the same way:
for example, in Ruby:
s = <<TEXT
!!Begin
Something haha
Title Good Bad Ugly
Date 1/1/2008
!!End
!!Begin
Other info
Title Iron Man
Date 2/2/2010
TEXT
result = s.scan(/^!!Begin.*?^Title\s+([^\n]*).*?^Date\s+([^\n]*)/m)
p result
result.each do |arr|
puts arr.join(' ')
end
output:
$ ruby try.rb
[["Good Bad Ugly", "1/1/2008"], ["Iron Man", "2/2/2010"]]
Good Bad Ugly 1/1/2008
Iron Man 2/2/2010
I'd use Perl with $/ = "!!End", then parse each paragraph.

Resources