def getDate():
result = open('test1.html', 'r')
data = re.findall(r"\"engine_jds\":(.+?),\"jobid_count\"",str(result.readlines()))
jsonObj = json.loads(data[0])
for item in jsonObj:
var = item["company_name"], item["job_name"], item["workarea_text"], item[
"providesalary_text"], item["companytype_text"], item["companysize_text"], item["jobwelf"]
# print(var)
print(var)
# return var
Output in the loop can output all data without being overwritten, but output outside the loop will be overwritten and only the last data can be output.
I don't know what you mean by "overwritten" here, but you do have one major problem:
... str(result.readlines())
The readlines function returns a list of strings, one for each line. The str() function will return a single string that looks like a Python list, with brackets and quotes:
"['Line one','Line two','Line three']"
You need to decide whether you want to handle the whole file at once (in which case you want result.read()), or if you want to handle the lines one by one, in which case you will need a loop.
Related
I have a string called listnumber
listnumbers
'1.0,2.0,3.0,4.0,5.0,6.0'
I have a function that returns each value of that string
def myfun(lists):
return ','.join([i for i in lists.split(',')])
When i type the function
myfun(listnumbers)
'1.0,2.0,3.0,4.0,5.0,6.0'
I have a loop script
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
lyr.definitionQuery="Value=" ""+myfun(listnumbers)+""
I end up getting
Value=1.0,2.0,3.0,4.0,5.0,6.0
What i would really like is for this to loop and give me
Value=1.0
Value=2.0
Value=3.0
and so on...... As separate entries. I feel i am very close i just need to make some changes.
Instead of joining the string back together, just leave it apart:
def myfun(lists):
return [i for i in lists.split(',')]
Then, in your loop, you should loop through the values of the list returned by myfun:
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
for value in myfun(listnumbers):
lyr.definitionQuery = "Value=" + value
However, str.split already does what this improved myfun does, since it already returns a list. Thus, you can simplify even further and get rid of myfun entirely:
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
for value in listnumbers.split(','):
lyr.definitionQuery = "Value=" + value
I’m having a problem with a dictionary. I"m using Python3. I’m sure there’s something easy that I’m just not seeing.
I’m reading lines from a file to create a dictionary. The first 3 characters of each line are used as keys (they are unique). From there, I create a list from the information in the rest of the line. Each 4 characters make up a member of the list. Once I’ve created the list, I write to the directory with the list being the value and the first three characters of the line being the key.
The problem is, each time I add a new key:value pair to the dictionary, it seems to overlay (or update) the values in the previously written dictionary entries. The keys are fine, just the values are changed. So, in the end, all of the keys have a value equivalent to the list made from the last line in the file.
I hope this is clear. Any thoughts would be greatly appreciated.
A snippet of the code is below
formatDict = dict()
sectionList = list()
for usableLine in formatFileHandle:
lineLen = len(usableLine)
section = usableLine[:3]
x = 3
sectionList.clear()
while x < lineLen:
sectionList.append(usableLine[x:x+4])
x += 4
formatDict[section] = sectionList
for k, v in formatDict.items():
print ("for key= ", k, "value =", v)
formatFileHandle.close()
You always clear, then append and then insert the same sectionList, that's why it always overwrites the entries - because you told the program it should.
Always remember: In Python assignment never makes a copy!
Simple fix
Just insert a copy:
formatDict[section] = sectionList.copy() # changed here
Instead of inserting a reference:
formatDict[section] = sectionList
Complicated fix
There are lots of things going on and you could make it "better" by using functions for subtasks like the grouping, also files should be opened with with so that the file is closed automatically even if an exception occurs and while loops where the end is known should be avoided.
Personally I would use code like this:
def groups(seq, width):
"""Group a sequence (seq) into width-sized blocks. The last block may be shorter."""
length = len(seq)
for i in range(0, length, width): # range supports a step argument!
yield seq[i:i+width]
# Printing the dictionary could be useful in other places as well -> so
# I also created a function for this.
def print_dict_line_by_line(dct):
"""Print dictionary where each key-value pair is on one line."""
for key, value in dct.items():
print("for key =", key, "value =", value)
def mytask(filename):
formatDict = {}
with open(filename) as formatFileHandle:
# I don't "strip" each line (remove leading and trailing whitespaces/newlines)
# but if you need that you could also use:
# for usableLine in (line.strip() for line in formatFileHandle):
# instead.
for usableLine in formatFileHandle:
section = usableLine[:3]
sectionList = list(groups(usableLine[3:]))
formatDict[section] = sectionList
# upon exiting the "with" scope the file is closed automatically!
print_dict_line_by_line(formatDict)
if __name__ == '__main__':
mytask('insert your filename here')
You could simplify your code here by using a with statement to auto close the file and chunk the remainder of the line into groups of four, avoiding the re-use of a single list.
from itertools import islice
with open('somefile') as fin:
stripped = (line.strip() for line in fin)
format_dict = {
line[:3]: list(iter(lambda it=iter(line[3:]): ''.join(islice(it, 4)), ''))
for line in stripped
}
for key, value in format_dict.items():
print('key=', key, 'value=', value)
I'm new to Python. Trying to make a simple function that converts a string input to braille via dict values (with '1' indicating a bump and 0 no bump).
I'm sure there are faster/better ways to do this but what I have is almost working (another way of saying it doesn't work at all).
alphabet = {
'a': '100000','b': '110000','c': '100100','d': '100110'
#etc
}
def answer(plaintext):
for i in str(plaintext):
for key, value in alphabet.iteritems():
if i in key:
print value,
answer('Kiwi')
This prints:
000001101000 010100 010111 010100
My question is how do I remove the spaces? I need it to print as:
000001101000010100010111010100
It's printing as a tuple so I can't use .strip().
For what I'd consider a pythonic way:
def answer(plaintext):
alphabet = { ... }
return "".join([alphabet[c] for c in plaintext])
(this assumes that all letters in plaintext are in alphabet)
brail = []
for i in str(...):
if alphabet.get(i):
brail.append(alphabet[i])
print ''.join(brail)
In the first line of the function, create a container l=[] .Change the last statement inside the loops to l.append(value).Outside the loops,but still inside the function, do return ''.join(l). Should work now.
You can use join() within a generator like this way:
alphabet = {'a': '100000','b': '110000','c': '100100','d': '100110'}
def answer(a = ''):
for i in a:
for key, value in alphabet.iteritems():
if i in key:
yield value
print ''.join(answer('abdaac'))
Output:
>>> 100000110000100110100000100000100100
One way to manipulate printing in Python is to use the end parameter. By default it is a newline character.
print("foo")
print("bar")
will print like this:
foo
bar
In order to make these two statements print on the same line, we can do this:
print("foo",end='')
print("bar",end='')
This will print like this:
foobar
Hopefully this is a helpful way to solve problems such as this.
I have a text file:
>name_1
data_1
>name_2
data_2
>name_3
data_3
>name_4
data_4
>name_5
data_5
I want to store header (name_1, name_2....) in one list and data (data_1, data_2....) in another list in a Python program.
def parse_fasta_file(fasta):
desc=[]
seq=[]
seq_strings = fasta.strip().split('>')
for s in seq_strings:
if len(s):
sects = s.split()
k = sects[0]
v = ''.join(sects[1:])
desc.append(k)
seq.append(v)
for l in sys.stdin:
data = open('D:\python\input.txt').read().strip()
parse_fasta_file(data)
print seq
this is my code which i have tried but i am not able to get the answer.
The most fundamental error is trying to access a variable outside of its scope.
def function (stuff):
seq = whatever
function('data')
print seq ############ error
You cannot access seq outside of function. The usual way to do this is to have function return a value, and capture it in a variable within the caller.
def function (stuff):
seq = whatever
return seq
s = function('data')
print s
(I have deliberately used different variable names inside the function and outside. Inside function you cannot access s or data, and outside, you cannot access stuff or seq. Incidentally, it would be quite okay, but confusing to a beginner, to use a different variable with the same name seq in the mainline code.)
With that out of the way, we can attempt to write a function which returns a list of sequences and a list of descriptions for them.
def parse_fasta (lines):
descs = []
seqs = []
data = ''
for line in lines:
if line.startswith('>'):
if data: # have collected a sequence, push to seqs
seqs.append(data)
data = ''
descs.append(line[1:]) # Trim '>' from beginning
else:
data += line.rstrip('\r\n')
# there will be yet one more to push when we run out
seqs.append(data)
return descs, seqs
This isn't particularly elegant, but should get you started. A better design would be to return a list of (description, data) tuples where the description and its data are closely coupled together.
descriptions, sequences = parse_fasta(open('file', 'r').read().split('\n'))
The sys.stdin loop in your code does not appear to do anything useful.
I want to append dictionary values to a list from a for loop, though it is only picking up the last value. Please advise, here is my code:
for line in obj:
test = float(line['value'])
print(test)
a = []
a.append(test)
Huh! You have messed up the indentation. Indentation is VERY IMPORTANT in python. a list is outside the for-loop hence it would only have the last value of test. IT should be inside the for-loop.
It should be like this -
a = []
for line in obj:
test = float(line['value'])
print(test)
a.append(test)
print a
Your call to append is outside the loop. Indentation matters in Python.