Printing a range of numbers - including end value [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have the following dictionary:
self.info("check_reg_min_max")
self.info(pprint.pformat(check_reg_min_max))
{
'TXH1DH50_DD70_03062018_FULL': [60398572, 60399376],
'TXH2DH50_DD70_03062018_FULL': [5071407, 5071709],
'TXH3DH50_DD70_03062018_FULL': [2822010, 2822116],
'TXH4DH50_DD70_03062018_FULL': [52148625, 52148782],
'TXH5DH50_DD70_03062018_FULL': [6764732, 6764766]
}
What I'm trying to do is write each range of numbers to a file, so I'm doing this:
for filename in check_reg_min_max:
jc.write(range(check_reg_min_max[filename][0], check_reg_min_max[filename][1] + 1))
The error message I keep getting is:
argument 1 must be string or read-only character buffer, not list
My understanding is that range takes two integers, and since it is inclusive, I am adding the +1 to grab the end value.
Any ideas what I am doing wrong?
EDIT: I grabbed the wrong error message. Error message updated.

The write() method expects a string. You can't pass it a range directly, it doesn't know what to do with that. This won't work:
f = open('test.txt', 'w')
f.write(range(1,10))
f.close()
You'd need to do something to convert that range to a string. This is one example of how you might do that, if you wanted a comma-separated list of values:
f = open('test.txt', 'w')
f.write(','.join([str(x) for x in range(1,10)]))
f.close()

Related

I'm getting a string in my list which should be a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm reading a csv file from my python code and expecting it to return a list of lists but I'm getting a list which contains a str. Can somebody tell me how I can convert it into a list thanks.
This is the code which is reading my csv file from my local directory.
with open('/home/developer/Desktop/csv/JE_csv/JP_Preisliste_GreenHome_Select_ab_29-10.2021.csv', encoding='utf-8-sig') as csv_data:
PriceListGetAG = list(csv.reader(csv_data, delimiter=';'))
for elem in range(0, len(GreenHomeSelect_PriceListGetAG), 1):
print(PriceListGetAG[elem])
This is what I'm getting.
["'14844', '26.10.2021', 'JP_C24_002', 'GreenHome Select', '01067'"]
This is what my expected output should be.
['14844', '26.10.2021', 'JP_C24_002', 'GreenHome Select', '01067']
I think your delimeter is comma. Change your parameter like this.
delimiter=','

I am trying to use the list I made in my getData function in my normalise function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
import string
def getData(filename):
with open(filename,'r') as f:
lines=[line.rstrip() for line in f]
lines=[x.lower() for x in lines]
return lines
filename="bibleSentences.txt"
getData(filename)
def normalize():
lowercase_lines=[x.lower() for x in lines]
return lowercase_lines
normalize(filename)
normalize()
I am trying to use the list I made in my getData function in my normalize function but I am getting an error saying that "lines is not defined" in the normalize function.
I am wondering how to call the function the right way so that I can avoid the error.
I guess you meant:
filename="bibleSentences.txt"
def normalize(filename):
lowercase_lines=[x.lower() for x in getData(filename)]
return lowercase_lines
normalize(filename)
What went wrong
When python returned the line variable to you, you didn’t assign it to anything, and therefore just lost it. A better way of doing it would be below.
Improved Code
filename="bibleSentences.txt"
lines = getData(filename)
def normalize():
lowercase_lines=[x.lower() for x in lines]
return lowercase_lines
lowercase_lines = normalize(lines)

inaccurately reading from file logic error Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I have the following text file on which I am using a csv reader:
number,obstacle,location,message
1,gobacktostart,8,Sorry but you've landed on a dangerous number, back to the start for you!
2,movetosquare42,matrix[1][0],The meaning of life is 42 or so they say, and that's where you're headed
I wish to (at the end) retrieve the number 8 from the row that starts 1,gobacktostart,8....etc.
My code is:
def gobacktostart():
with open("obstacles.txt","r") as f:
idnumber="1"
fReader=csv.reader(f)
for row in fReader:
for field in row:
if field==idnumber:
print(row[3])
player1position==row[2]
print(player1position)
and the undesired output however, is:
>>>
Sorry but you've landed on a dangerous number
1
>>>
I do need to read the value into the variable player1position in order to pass it on to another function at a different part of the program.
Any thoughts on solving this logic error? Why is it printing "1", when row[2] refers to the 8. Also, Row[3] seems to execute properly in the previous line....
You are checking for equality not assignment in player position == row[2]

How to return the average from a file in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This should return the average of all of the values in the file. This is what I have. I get an error, could anyone help me out? Thank you!
def averageValueOnFile(fileName):
with open('fileName.txt') as f:
sum=0
count=0
for line in f:
count+=1
sum+=float(line.split(":")[1])
average=sum/count
print (average)
IndexError: list index out of range
What do the contents of the file look like?
One or more of your lines in the file does not have a : or is simply blank after the colon
Would sum+=float(line.split(":")[0]) work instead?
Your error likely comes from the fact that one of the lines in the file is not formatted as you expect: the split method returns a list with less than two elements.

Code works fine when print statement is present, but throws error when I comment the print [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having really strange error. I am using Python 2.7 on Linux Mint OS. Here is the code:
with open(edgeClusteringPath) as f:
for line in f:
clustId = str(currCluster)
spl = line.split(" ")
# print spl
for i in spl:
# print i
(first, second) = edgeNodes[int(float(i))]
nodeClusters.setdefault(first, set())
nodeClusters.setdefault(second, set())
nodeClusters[first].add(clustId)
nodeClusters[second].add(clustId)
# print i
currCluster += 1
edgeClusteringPath is a path to a space separated data and edgeNodes is a dictionary with key = edgeId and value = (firstNode, secondNode). The problem occurs in the second for loop. This code gives me error:
> line 34, in <module>
(first, second) = edgeNodes[int(float(i))]
KeyError: 0
But when I uncomment one (or both) of the print i statements, the execution works fine (without error). It is really strange, I do not see how the print statement could affect the remaining code. I could uncomment the print statement, but I do not actually need that printing, so I would rather get rid of that line.
Here is a sample of edgNodes:
87388: (3250, 6041), 87389: (3250, 6045), 87390: (3250, 6046)
It is a huge dictionary, so I extracted only 3 key-value pairs.
Thank you in advance!
Edit:
Now my code works. I had problem with the paths I have been using. I used wrong file to initialize edgeNodes dictionary and it caused the problems. However, I posted the question just to see if anybody had idea how adding one line changes the behavior of the code. I have been using Java for 3 years and never had similar issue. I am new to Python and do not know how it works internally, so I wished to know if anybody has idea about the effect on one line of the other code.
Thank you for your opinions and suggestions!

Categories