Deprecated: Required parameter $cat_id follows optional parameter $type in /www/wwwroot/ebimall.com/systems/hong.php on line 2088

Deprecated: Required parameter $where follows optional parameter $tree_id in /www/wwwroot/ebimall.com/systems/hlb.php on line 3505
Python Script Unable to Get Cell Values Stack Overflow188bio精品生物—专注于实验室精品爆款的电商平台 - 蚂蚁淘旗下精选188款生物医学科研用品
您好,欢迎您进入188进口试剂采购网网站! 服务热线:4000-520-616
蚂蚁淘商城 | 现货促销 | 科研狗 | 生物在线

Python Script Unable to Get Cell Values Stack Overflow

IamtryingtoaccomplishaseeminglysimpletaskinPython.ThisisnowmythirdSOpostinasmanydaysandIamembarassed.

Iamhopingtoopenacsvfile.Iwouldliketoloopthrougheachrow,andforeverycolumnineveryrow,ifthevalueisNOTa0ora1(itisa"?"),toessentiallyrewritethatvaluewiththevalueoftheverylastcolumn.Theverylastcolumncannevernotbea0or1.Itwillneverbe"?".Iamnotsomuchconcernedwiththeprintstatement,asIamreplacingthe"?"(ornon0/1)valuewiththevalueinthelastcolumn.

IhaveattachedanexamplecsvfileIamworkingwithhere:http://www.sharecsv.com/s/7bef636c33054cae624928297146bae1/house.csv

Ifyouareunabletoviewthelinkabove,IhavepastedaSAMPLEofthedatasetbelow:

0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,1.00?,1.00,1.00,?,1.00,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00,0.00,0.000.00,1.00,1.00,0.00,?,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.00,0.00,1.00,0.001.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,1.00,0.000.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,?,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.000.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,1.00,1.00,0.00,0.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,?,?,1.000.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,?,?,0.001.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,?,1.00,1.00,?,0.00,0.00,1.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,?,0.00,?,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,?,0.00,?,1.001.00,0.00,1.00,0.00,0.00,1.00,0.00,1.00,?,1.00,1.00,1.00,?,0.00,0.00,1.00,0.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,?,?,1.00,1.00,0.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,?,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,0.00,1.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,1.00,0.00,1.00,1.001.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,1.00,0.00,0.00,0.00,1.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,0.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.000.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.001.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,1.00,?,1.00,0.00,0.00,1.00,1.00,0.00,1.00,0.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,?,1.001.00,1.00,1.00,0.00,0.00,?,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,?,0.00,0.00,0.00,1.00,0.001.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,0.001.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00

Hereismycurrentcodes:

import csvreader = csv.reader(open("house1.csv"), delimiter = ",")counter = 0for row in reader: # print("Opened Reader") currVal = row[:-1] counter = counter + 1 # print("set values") for column in row: questioncount = 0 # print("Looping columns") if (column != 0 or column != 1): questioncount = questioncount + 1 # This is where I should overwrite the value print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")

Idon"tunderstandwhyIamhavingsomuchdifficulty.Currently,theoutputofPycharmisthatitsayseverysinglerowandcolumnhasaquestionmark,whichisnottrue.

Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 435 has 1 question marks

Iwouldhopethatgivenalinelike
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.00
thispythonscriptwouldturnthatlineinto
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.00

Anyadvicewouldbeappreciated.


@EshakaIalreadyincludedanexternallinktowheeIhaveit,butIjusteditedthequestiontoincludeachunks.wundermahnFeb12"19at1:16
Ifthisisyouractualcode,yourprintstatementisnotindentedproperly.Itsjustprintingthesamelineforeveryrowandeverycolumn,bypassingyourifblockaltogether.kerweiFeb12"19at1:21
@kerweithisismyactualcode,andIcanadjusttheprintstatement.Thankyoufornoticingmyerror.Iamhowevermostlyfocusedontryingtoreplacethe?valuesanddontevenreallyplanonprintinganything,asindicatedinthequestionbody.wundermahnFeb12"19at1:23

Yourcodehasgottwobugs.Thefirstoneisthatyoucheckifacolumndoesn"tequaltointegers0or1,butcolumncontainsastringvalue.Thesecondone,theinitializationofthequestioncountvariableandacallofprintfunctionshouldtakeplaceoutofthefor-loop.Hereisaworkingcode:

import csv
rows.append(row) print("Row {i} has {q} question marks".format(i=counter, q=questioncount))

P.S.:I"veupdatedthecodesothatnowitsavesanotherfilewithquestionmarksbeingreplaced.


Hi,@constt.Thankyouforthis.Ijusttesteditandtheprintstatementworksgreat,awesome!However,asmyinitialquestionactuallyrequested,Icarenotsomuchfortheprintstatement,asIdoforbeingabletoreplaceaquestionmarkvaluewiththevalueinthelastcolumn.So,Iamunabletoselectyouasansweringthequestion.wundermahnFeb12"19at1:34

Youwerecheckingifthestringvalues(fromcsvfile)"1.00"or"0.00"areequaltotheintegervalues(inyourifstatement)1or0.

Also,youshouldapply"not"tothetwochecksatonceotherwiseyourlogicfallsthrough.


You"rereadingthe.csv,butthecontentswillbereadasstrings,notasfloats,soyourcomparisonif(column!=0)willalwaysbetrue.Asaresult,youalwayscount1questionmark,whichyouthenproceedtoprintonceforeverycolumn,astheprintstatementisinthecolumnloop,nottherowloop.YouareassigningthelastvaluetocurrVal,butneverusingit.

Sinceyourcodeislookingforquestionmarks,perhapsyoushouldnotcheckwhetheracolumnisn"t1or0,butwhetheritis"?"instead?

Yourcodewiththosefixes:

import csvreader = csv.reader(open("house1.csv"), delimiter=",")counter = 0for row in reader: counter = counter + 1 questioncount = 0 for column in row: if column == "?": questioncount += 1 print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")

Towritetheoutputyou"relookingfor:

import csvreader = csv.reader(open("simple.csv"), delimiter=",")writer = csv.writer(open("output.csv", "w", newline=""), delimiter=",")for row in reader: writer.writerow([column if column != "?" else row[-1:] for column in row])

interesting, thank you. Initially I tried looking for question marks but used ? and not ? . This definitely works, and is a clean solution (as always). Thank you for your suggestion. In a way of trying to go back to what my question was hopefully trying to ask, is how can I now write to the SAME csv file to replace those question marks with the value in the last column? That is what I was trying to achieve, just using print statements to test, as somebody suggested me to some days ago ;) wundermahn Feb 12 "19 at 1:40
I ve added that solution as well, keeping to your initial idea of opening the file and creating the reader and writer in one go. I would recommend using with if this code is part of a larger project. Note that you should get the same results regardless of using ? or ? . Grismar Feb 12 "19 at 1:46
Hi, @Grismar. Thank yo for this. Would you mind offering an explanation for writer.writerow([column if column != ? else 1.00 for column in row]) ? How does this grab the value of the last column in that row, doesn t this assume it is always 1? Also, this is note, just trying to create a suite of data cleaning tools and scripts I can use as I continue to practice with Python and Machine Learning. wundermahn Feb 12 "19 at 1:48
Oh I m sorry, I missed that aspect, you want ? to become equal to the last. Let me update the example. Note that it now writes row[-1:] for each ? it finds. Grismar Feb 12 "19 at 1:52
site design / logo 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2020.5.18.36843
StackOverflowworksbestwithJavaScriptenabled

新闻动态
行业前沿
技术文章
最新产品

188进口试剂采购网 www.188bio.cn -中国试剂网,试剂网,化学试剂网,国药试剂,抗体公司,试剂公司,试剂盒公司,苏州试剂公司,北京化学试剂公司,天津化学试剂,试剂商城,试剂代理,流式抗体 细胞库查询 sitemap