for line in open('transactions.dat','r'):
item=line.rstrip('\n')
item=item.split(',')
custid=item[2]
amt=item[4]
if custid in cust1:
a=cust1[custid]
b=amt
c=(a)+(b)
print(cust1[custid]+" : "+a+" :"+b+":"+c)
break
else:
cust1[custid]=amt
Output:
85.91 : 85.91 :85.91:85.9185.91
Well above is my code what I want is
when I read from a file I want to add the customer amount with same
id.
Secondly there should not be repetition of customer id in my
dictionary.
so I am trying to add customer amount which is c but it gives me appended string instead of adding the two. You can see in the last part of my output which is value of c. So how do I add the values.
Sample transaction data:
109400182,2016-09-10,119257029,1094,40.29
109400183,2016-09-10,119257029,1094,9.99
377700146,2016-09-10,119257029,3777,49.37
276900142,2016-09-10,135127654,2769,23.31
276900143,2016-09-10,135127654,2769,25.58
You reading strings, instead of floats, from the file. Use this amt=float(item[4]) to convert strings representing numbers to floats, and then print(str(cust1[custid])+" : "+str(a)+" :"+str(b)+":"+str(c)) to print out.
Your code may need lots of refactor, but in a nutshell and if I understand what you are trying to do you could do
c = float(a) + float(b)
and that should work.
Related
I have been trying to figure out what the issue is and can't seem to figure it out.
valueRange = [j.value for i in copyRange for j in i]
vrCounter = 0
vrStep = 7
x.field_names = ["Jurisdiction","SPOC-Name", "Lines of Business","Market Area", "Consultant Personal Lines", "Consultant Business Lines", " ROC-Name"]
for i in range(0,len(valueRange)):
x.add_row(valueRange[i], valueRange[i+1], valueRange[i+2], valueRange[i+3], valueRange[i+4], valueRange[i+5], valueRange[i+6])
print(x)
I ran the code without the x.add_row() function and it printed the values correctly. The valueRange list is just a bunch of keywords that match the field_names.
As shown in the docs, add_row() takes a list of values as an argument.
Enclose all the values in a list and it must work.
x.add_row([valueRange[i], valueRange[i+1], valueRange[i+2], valueRange[i+3], valueRange[i+4], valueRange[i+5], valueRange[i+6]])
Turn out the issue was the for i in range(0,len(valueRange)): portion. So it wasn't possible to answer this question without complete information about that valueRange list. This list contained 28 (0-27) string values. The loop was stepping through the 0-27 range 1 at a time while trying to apply an i+number value to each one. This would quickly go out of bounds unless the step size matches the valueRange list.
The final solution looks like:
for i in range(0,len(valueRange),7):
x.add_row(valueRange[i:i+7])
I am just a beginner in the Python so kindly excuse for this question, I tried a lot to get it done, but failed, thus I am posting this. I have a data set which looks like:
5.96303e-07 (11.6667 3.21427 -2.20471e-07) (11.8746 -1.75419 -2.37923e-07) (8.66991 -2.84873 5.29442e-07) (2.19427 13.547 1.16203e-05)
9.67139e-07 (11.6171 3.16081 -8.83286e-08) (11.8851 -1.763 -4.38136e-07) (8.68988 -2.85339 1.81039e-07) (1.61058 13.629 4.42662e-07)
1.34613e-06 (11.5562 3.11037 -7.74061e-08) (11.8897 -1.77006 -3.81523e-07) (8.70652 -2.8608 8.00436e-08) (1.47268 13.5569 -2.03173e-06)
1.73261e-06 (11.4961 3.06921 -1.49294e-07) (11.8919 -1.77567 -3.48887e-07) (8.71974 -2.86802 5.2652e-08) (1.59798 13.4556 -2.52073e-06)
2.12563e-06 (11.4423 3.03706 -1.53771e-07) (11.8932 -1.78022 -3.33928e-07) (8.73 -2.87398 4.65075e-08) (1.77817 13.3679 -2.42045e-06)
Now when I am accessing the data frame for an instance df.iloc[:,1] it gives me (11.6171, when I tried to plot it --it gives me error, then I thought that since the "(" is creating a problem I removed that using df.replace('\(','',regex=True).replace('\)','',regex=True) . The plot function seems to work but gives very weird figure(not allowed to post the figure). In addition to that when I tried to do some calculations like (df.iloc[:,1])^2 it is giving me errors which says:
TypeError: can't multiply sequence by non-int of type 'str'
I guess the data is not in the correct form. Any comment or suggestion will be a great help. Thanks in advance.
There are two relatively minor issues. Something like the following might be what you're looking for. Maybe.
First, the column you are trying to plot is a string. Essentially it contains letters/symbols. Even when you remove the "(" ")" the "numbers" are still considered a string.
# To convert a "3.14" (string) to a 3.14 (float)
# floats are basically decimals
my_string = "3.14"
my_number = float(my_string)
Additionally, there are multiple "numbers" in the string. So to plot the numbers in that column, I think you would first need to split the string and then convert to numbers.
# Use your code to replace the special characters
df.replace('\(','',regex=True).replace('\)','',regex=True)
# new data frame with split value columns
new = df["colname_with_three_numbers"].str.split(" ", n = 2, expand = True)
# Making separate first name column from new data frame
df["first_number"]= new[0]
df["second_number"]= new[1]
df["third_number"]= new[2]
# change the type to allow you to plot something like this should work
df["first_number"] = float(df["first_number"])
df
This is a pretty bad method to solve this, but if the dataset is not too large you can get each element using a for loop and remove the brackets using str.replace(")","").
I have a problem with this
def randomrecipes():
print (random.sample(listofrecipes, 1))
mainmenu()
listofrecipes = [monday, tuesday, wednsday, thursday, friday, saturday, sunday]
the recipes is formatted like this (just ignore the Norwegian gibberish).
monday"""
1 l kjøttkraft /buljong eller lettsaltet vann
750 g rå potet
300 g kokt potet
3 dl byggmel
2 ss hvetemel
1 ts salt
"""
When I just print it, it comes out with the whitespace preserved, but from randomrecipes I get this:
['\n1 l kjøttkraft /buljong eller lettsaltet vann\n750 g rå potet\n300 g kokt potet\n3 dl byggmel\n2 ss hvetemel\n1 ts salt\n']
Im trying to learn by myself and is just getting started, so sorry if this is a dumb question. Thanks.
You're using random.sample(iterable, k) which always returns a list of length k or in your case 1. So the object you're printing is a list of a string.
You could change the line
print (random.sample(listofrecipes, 1))
to something like
print(random.sample(listofrecipes,1)[0]) # note the [0]
That will return the first element in the list (which happens to be the only one).
A cleaner solution might be instead to use random.choice(iterable). It always returns a single choice, not a list. It is effectively equivalent as the line above, but it's a little clearer.
random.sample returns a list, which is exactly what you're getting. This output is just how lists are formatted. If you want to extract the string from this list, do it:
random.sample(listofrecipes, 1)[0]
The output of random.sample is a list of a certain length, 1 in this instance. When you print this you are seeing the output that is generated when displaying a list, including newline \n characters.
print(random.sample(listofrecipes, 1)[0])
would print out the string, the first element of the list, which would reinstate the original display across several lines.
I'm pretty new to Python, and put together a script to parse a csv and ultimately output its data into a repeated html table.
I got most of it working, but there's one weird problem I haven't been able to fix. My script will find the index of the last column, but won't print out the data in that column. If I add another column to the end, even an empty one, it'll print out the data in the formerly-last column - so it's not a problem with the contents of that column.
Abridged (but still grumpy) version of the code:
import os
os.chdir('C:\\Python34\\andrea')
import csv
csvOpen = open('my.csv')
exampleReader = csv.reader(csvOpen)
tableHeader = next(exampleReader)
if 'phone' in tableHeader:
phoneIndex = tableHeader.index('phone')
else:
phoneIndex = -1
for row in exampleReader:
row[-1] =''
print(phoneIndex)
print(row[phoneIndex])
csvOpen.close()
my.csv
stuff,phone
1,3235556177
1,3235556170
Output
1
1
Same script, small change to the CSV file:
my.csv
stuff,phone,more
1,3235556177,
1,3235556170,
Output
1
3235556177
1
3235556170
I'm using Python 3.4.3 via Idle 3.4.3
I've had the same problem with CSVs generated directly by mysql, ones that I've opened in Excel first then re-saved as CSVs, and ones I've edited in Notepad++ and re-saved as CSVs.
I tried adding several different modes to the open function (r, rU, b, etc.) and either it made no difference or gave me an error (for example, it didn't like 'b').
My workaround is just to add an extra column to the end, but since this is a frequently used script, it'd be much better if it just worked right.
Thank you in advance for your help.
row[-1] =''
The CSV reader returns to you a list representing the row from the file. On this line you set the last value in the list to an empty string. Then you print it afterwards. Delete this line if you don't want the last column to be set to an empty string.
If you know it is the last column, you can count them and then use that value minus 1. Likewise you can use your string comparison method if you know it will always be "phone". I recommend if you are using the string compare, convert the value from the csv to lower case so that you don't have to worry about capitalization.
In my code below I created functions that show how to use either method.
import os
import csv
os.chdir('C:\\temp')
csvOpen = open('my.csv')
exampleReader = csv.reader(csvOpen)
tableHeader = next(exampleReader)
phoneColIndex = None;#init to a value that can imply state
lastColIndex = None;#init to a value that can imply state
def getPhoneIndex(header):
for i, col in enumerate(header): #use this syntax to get index of item
if col.lower() == 'phone':
return i;
return -1; #send back invalid index
def findLastColIndex(header):
return len(tableHeader) - 1;
## methods to check for phone col. 1. by string comparison
#and 2. by assuming it's the last col.
if len(tableHeader) > 1:# if only one row or less, why go any further?
phoneColIndex = getPhoneIndex(tableHeader);
lastColIndex = findLastColIndex(tableHeader)
for row in exampleReader:
print(row[phoneColIndex])
print('----------')
print(row[lastColIndex])
print('----------')
csvOpen.close()
I have a list import from a data file.
lines=['1628.246', '100.0000', '0.4563232E-01', '0.4898217E-01', '0.3017656E-02', '0.2271272', '0.2437533', '0.1500232E-01', '0.4102987', '0.4117742', '0.5461504E-02', '2.080838', '0.5527303E-03', '-0.4542367E-03', '-0.2238781E-01', '-0.8196812E-03', '-0.3796306E-01', '-0.7906407E-03', '-0.6738000E-03', '0.000000']
I want to generate a new list include all element in same 10 digits and put back to file
Here is I did:
newline=map(float,lines)
newline=map("{:.10f}".format,newline)
newline=map(str,newline)
jitterfile.write(join(newline)+'\n')
It works, but looks not beautiful. Any idea to make it good looking?
You can do it in a single line like so:
newline=["{:.10f}".format(float(i)) for i in lines]
jitterfile.write(join(newline)+'\n')
Of note, your third instruction newline=map(str,newline) is redundant as the entries in the list are already strings, so casting them is unnecessary.
The map function also accept lambda , also as the result of format is string you don't need to apply the str on your list ,and you need to use join with a delimiter like ',':
>>> newline=map(lambda x:"{:.10f}".format(float(x)),newline)
>>> newline
['1628.2460000000', '100.0000000000', '0.0456323200', '0.0489821700', '0.0030176560', '0.2271272000', '0.2437533000', '0.0150023200', '0.4102987000', '0.4117742000', '0.0054615040', '2.0808380000', '0.0005527303', '-0.0004542367', '-0.0223878100', '-0.0008196812', '-0.0379630600', '-0.0007906407', '-0.0006738000', '0.0000000000']
jitterfile.write(','.join(newline)+'\n')