Hi I am new to Python and Jupiter NB = I am doing an exercise in using the "".join()
def combine(words):
sentence = ['Hello',', ','how',' ','are',' ','you','?']
sentence = "".join(words)
return sentence
but when I run this it does not give me an output.
Related
import json
#Load the data into a list.
filename = 'population_data.json'
with open(filename)as f:`enter code here`
pop_data = json.load(f)
enter code here
#Print the 2010 population data for each country.
for pop_dict in pop_data:`enter code here`
if pop_dict['Year'] == '2010':
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
print(country_name + " : " + str(population))
Am trying to extract data from a population_data.json file, but whenever i run my code it doesn't show any result and i don't get any Errors, i have save the population data file in the same folder with the code but i still have that same problem, i don't get any result of the data in the shell. i would be glad if someone can help.Thank you .
enter code here
import json
#Load the data into a list.
filename = 'population_data.json'
with open(filename)as f:`enter code here`
pop_data = json.load(f)
enter code here
#Print the 2010 population data for each country.
for pop_dict in pop_data:`enter code here`
if pop_dict['Year'] == '2010':
country_name = pop_dict['Country Name']
population = int(float(pop_dict['Value']))
print(country_name + " : " + str(population))
I would suggest starting your script in the python debugger (pdb). You can start it either by starting your script like this:
python3 -m pdb your_script.py
or by importing the pdb module. Adding the following line into your python file (for example after import json, or any other):
import pdb; pdb.set_trace()
Once it is loaded, the debugger stops at the first instruction and shows the debugger promt (Pdb) . Continue execution line by line by typing next and hitting ENTER until you hit a line where something interesting is happening. The debugger prints always the next instruction and the script path and line.
Debugger output is added below, everything after (Pdb) you have to type and confirm with ENTER
(Pdb) next
> /path/to/your_script.py(7)<module>()
-> pop_data = json.load(f)
(Pdb) next
> /path/to/your_script.py(11)<module>()
-> for pop_dict in pop_data:
Now let the debugger print contents of a variable
(Pdb) p pop_data
{"Probably": "something", "you": "don't expect here"}
I suspect either the for loop yields 0 pop_dicts and therefore the loop body is never executed, or no pop_dict key Year has value 2010 and the if body is never executed.
Alternative to type next often (== single stepping): set a break point on a specific line (your_script.py:11), and continue execution until the breakpoint is hit
(Pdb) break your_script.py:11
Breakpoint 1 at /path/to/your_script.py:11
(Pdb) continue
> /path/to/your_script.py(11)<module>()
-> for pop_dict in pop_data:
(Pdb)
For additional debugger commands see pdb commands
i have some code to make unigram, bigram, and trigram from some sentences, but i want this code can make it from file.txt , im newbie in programing let me know what i must do ?
def ngrams(s, n=2, i=0):
while len(s[i:i+n]) == n:
yield s[i:i+n]
i += 1
txt ='Python is one of the awesomest languages'
unigram = ngrams(txt.split(), n=1)
a = list(unigram)
bigram = ngrams(txt.split(), n=2)
b = list(bigram)
trigram = ngrams(txt.split(), n=3)
c = list(trigram)
print('unigram:')
print(a)
print('bigram:')
print(b)
print('trigram:')
print(c)
When you want to write a file (notice the 'w' in the open statement):
with (open('some.txt', 'w')) as file:
file.write("this is an example!")
When you want to read a file (notice the 'r' in the open statement):
with (open('some.txt', 'r')) as file:
line = file.readline();
print(line)
This should be a good starting point for you.
I'm using Modeler 18.0 and I'm new to the tool.
I inherited 30 stream files. Each stream starts with 30 excel source nodes with file names like \.xlsx (e.g. c:\source\regl_sales_01_WI_2017Q3.xlsx). I need to update all 900 nodes for the 2017Q4 versions of the source files.
Can I do this with some type of script where I can find and replace? Would this be a stand alone script? Seems like I could use something like node.setPropertyValue("full_filename", "c:\source\regl_sales_01_WI_2017Q3.xlsx") If I can only identify the script and node.
Thank you
I think your problem can be solved using standalone scripting and the stream.findAll()-functionality.
You could do something like this:
from os import listdir
from os.path import isfile, join
session = modeler.script.session()
tasks = session.getTaskRunner()
mypath = 'C:\\yourpath'
streams = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and f.endswith(".str"))]
for streamFile in streams:
print(stream.getName()+" is getting processed.")
stream = tasks.openStreamFromFile(demosDir + streamFile, True)
inputNodes = stream.findAll("excelimport", None)
for in in inputNodes:
ff = in.getPropertyValue("full_filename")
ff.replace("2017Q3.xlsx", "2017Q4.xlsx")
in.setPropertyValue("full_filename", ff)
print(stream.getName()+" is processed.")
stream.close()
I did not test this, but it shouldn't need a lot of tweaking to work.
For my classroom, I have a PN532 NFC card reader/writer hooked up via UART to a Raspberry Pi 2, and I'm using Type 2 NXP NTAG213 NFC cards to store information specifically to the text record. While weak in Python, I used the example under subheader 8.3 in the NFCPy Documentation to write to the card and used "How to redirect 'print' output to a file using python?" in order to complete the output process to a text file. For a while, the reading, writing, and outputting to my text file worked:
import nfc
import nfc.ndef
import nfc.tag
import os, sys
import subprocess
import glob
from os import path
import datetime
f = open('BankTransactions.txt', 'a')
sys.stdout = f
path = '/home/pi/BankTransactions.txt'
def connected(tag): print(tag); return False
clf = nfc.ContactlessFrontend('tty:AMA0:pn532')
clf.connect(rdwr={'on-connect': connected})
tag = clf.connect(rdwr={'on-connect': connected})
record_1 = tag.ndef.message[0]
signature = nfc.tag.tty2_nxp.NTAG213
today = datetime.date.today()
print(record_1.pretty())
if tag.ndef is not None:
print(tag.ndef.message.pretty())
if tag.ndef.is_writeable:
text_record = nfc.ndef.TextRecord("Jessica has 19 GP on card")
tag.ndef.message = nfc.ndef.Message(text_record)
print >> f, "Edited by Roman", today, record_1, signature, '\n'
f.close()
Now, however, when I use the same card for testing, it will not append the data within the text file. The data is still being written to the card, as I can read the information on the card with a simple read program.
I am reading the input of the MATLAB code from an Excel file using xlsread, after the calculation I am exporting to Word for writing out the report(writetoword.m). In Excel, there is a string which I should read in MATLAB and output in Word.
In Excel file(input.xlsx) it is written 'shoe'.
I read using
[num,txt,raw] = xlsread('input.xlsx');
eng = txt(14,19); % the word 'shoe' in that excel cell
In writetoword.m, I wrote,
test = eng;
WordText(ActXWord,test,Style,[0,1]);
function WordText(actx_word_p,text_p,style_p,enters_p,color_p)
if(enters_p(1))
actx_word_p.Selection.TypeParagraph;
end
actx_word_p.Selection.Style = style_p;
if(nargin == 5)
actx_word_p.Selection.Font.Color=color_p;
end
actx_word_p.Selection.TypeText(text_p);
actx_word_p.Selection.Font.Color='wdColorAutomatic';
for k=1:enters_p(2)
actx_word_p.Selection.TypeParagraph;
end
return
It is not printing anything. The error is in the line
actx_word_p.Selection.TypeText(text_p);
now if I write
test = 'eng';
WordText(ActXWord,test,Style,[0,1]);
It will come as eng and not shoe.
How can I fix this problem?
You're assigning txt as a cell instead of a string. Use eng = txt{14,19}; instead.