Number of cells, using CSV module
I'm using the csv module to get information from a csv file. I have items
listed in Column A. I want to know how many items are listed in Column A.
import csv
with open('test.csv', 'r') as f:
reader = csv.reader(f)
for column in reader:
column = (column[0])
print(column)
We are given
>a
>b
>c
>d
>e
>f
How would I go about getting the amount of numbers that are printed? If I try
printing len(column), I get
>1
>1
>1
>1
>1
>1
--
http://mail.python.org/mailman/listinfo/python-list
Re: Number of cells, using CSV module
On Thursday, May 16, 2013 2:40:08 PM UTC-4, Skip Montanaro wrote: > Perhaps you want len(reader) instead? Or a counter which increments for > every row read which has an item in column A? > > > > Skip len(reader) gives me an error. I tried a counter, but unfortunately due to the simplicity of CSV files, any formulas are not saved. -- http://mail.python.org/mailman/listinfo/python-list
Re: Number of cells, using CSV module
I guess another way to accomplish this would be, is there any way that I can turn the returned value for (column) into 1 list? So rather than >a >b >c >d >e >f I would get [a, b, c, d, e, f] -- http://mail.python.org/mailman/listinfo/python-list
Re: Number of cells, using CSV module
Thank you Skip, worked great. And thank you Tim for Tidying things up! -- http://mail.python.org/mailman/listinfo/python-list
TypeError: Can't convert 'int' object to str implicitly
Hey,
Let me explain what my program is supposed to do...
I am using a macro program in conjunction with a python script I am writing.
The macro inputs data into a database we use at my job, blah blah blah.
The script asks how many numbers (devices) you need to enter. Next, it asks you
to input the device numbers. When you input them, it creates a list with all of
the devices. I then tell it to go into the script of the Macro (firstdev.ahk)
that will run on the back-end, and replace the word "device" with the first
device in the list. It then should execute the Macro, change the device number
back to the word "Device" for future use, and then delete the first number from
the list. It will repeat as long as there are numbers in the list.
The error I receive is "TypeError: Can't convert 'int' object to str
implicitly" when it tries to put the device into the macro script. It worked
fine when I just had it input one device into the script without the use of
lists, but for whatever reason, using a list does not play nice with replacing
the words "Device" with the number from the list. Here is my script. I will
give you up until the part that the error occurs, as everything afterwords is
pointless.
I am fairly new to python, so if anything looks screwed up or like I am an
idiot, it is because I am.
import fileinput, sys, os
devlist = []
maxdev = int(input("How many devices to add: "))
curdev = int("0")
while curdev < maxdev:
try:
Number = int(input("Enter Device number: "))
devlist.append(Number)
curdev = curdev + 1
except ValueError:
print("Please enter a valid number")
ready = 0
while ready != "Y" and ready != "y" and ready != "yes" and ready != "YES" and
ready != "ready" and ready != "Ready":
try:
ready = input("Confirm when you are ready ")
except ValueError:
print("shit broke. ")
##This next step will seek out the word Device within firstdev.ahk, and replace
with devlist[0]
for line in fileinput.input(["firstdev.ahk"], inplace=True):
line = line.replace("device", devlist[0])
sys.stdout.write(line)
##next step runs firstdev.ahk
os.system('firstdev.ahk')
##next step is replacing devlist[0] with "device"
for line in fileinput.input(["firstdev.ahk"], inplace=True):
line = line.replace(devlist[0], "device")
sys.stdout.write(line)
del devlist[0] #deleting the first item from the list. next steps will repeat.
Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: Can't convert 'int' object to str implicitly
On Friday, April 26, 2013 10:53:44 AM UTC-4, Peter Otten wrote: > [email protected] wrote: > > > > > Hey, > > > > > > Let me explain what my program is supposed to do... > > > > > > I am using a macro program in conjunction with a python script I am > > > writing. The macro inputs data into a database we use at my job, blah blah > > > blah. > > > > > > The script asks how many numbers (devices) you need to enter. Next, it > > > asks you to input the device numbers. When you input them, it creates a > > > list with all of the devices. I then tell it to go into the script of the > > > Macro (firstdev.ahk) that will run on the back-end, and replace the word > > > "device" with the first device in the list. It then should execute the > > > Macro, change the device number back to the word "Device" for future use, > > > and then delete the first number from the list. It will repeat as long as > > > there are numbers in the list. > > > > > > The error I receive is "TypeError: Can't convert 'int' object to str > > > implicitly" when it tries to put the device into the macro script. > > > > Python is trying hard to give you a meaningful error message and shows the > > line causing the error in the traceback. It pays to read carefully -- or to > > post it here if it doesn't make sense to you. > > > > > devlist = [] > > ... > > > Number = int(input("Enter Device number: ")) > > > devlist.append(Number) > > ... > > > line = line.replace(devlist[0], "device") > > > > devList is a list of integers, and devlist[0] is thus an int. > > > > >>> "foo device bar\n".replace(42, "device") > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: Can't convert 'int' object to str implicitly > > > > Implicitly? So let's try and convert the int /explicitly/ . > > > > >>> "foo device bar\n".replace(str(42), "device") > > 'foo device bar\n' > > > > No error, but probably still not what you expected. Can you sort it out > > yourself? > > > > > I am fairly new to python, so if anything looks screwed up or like I am an > > > idiot, it is because I am. > > > > I like to see a bit of self-deprecation now and then, but on this list > > complete tracebacks find even more love ;) Thank you, Peter. This was a tremendous help. Got it working. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: Can't convert 'int' object to str implicitly
On Friday, April 26, 2013 11:05:29 AM UTC-4, Chris Angelico wrote:
> On Sat, Apr 27, 2013 at 12:26 AM, wrote:
>
> > ##This next step will seek out the word Device within firstdev.ahk, and
> > replace with devlist[0]
>
> > for line in fileinput.input(["firstdev.ahk"], inplace=True):
>
> > line = line.replace("device", devlist[0])
>
> > sys.stdout.write(line)
>
> > ##next step runs firstdev.ahk
>
> > os.system('firstdev.ahk')
>
> > ##next step is replacing devlist[0] with "device"
>
> > for line in fileinput.input(["firstdev.ahk"], inplace=True):
>
> > line = line.replace(devlist[0], "device")
>
> > sys.stdout.write(line)
>
>
>
> I've checked out what fileinput.input() is doing here (ought to have
>
> done that earlier, sorry!) and I now understand this block of code
>
> more. You're replacing that word _in the file, on disk_, and then
>
> making the inverse replacement. This strikes me as dangerous; if
>
> anything happens to your process in the middle, the file will be
>
> damaged on disk. I would STRONGLY recommend rewriting this to use some
>
> other file - for instance, a temporary file. I haven't looked into the
>
> details, as I haven't actually done this lately in Python, but you
>
> should be able to use tempfile.NamedTemporaryFile(delete=False) [1],
>
> write to it, make it executable, run it, and unlink it. That way,
>
> you're creating a temporary file to run, not running the original.
>
> This is semantically different from your code, but I think it'd be a
>
> lot safer.
>
>
>
> [1]
> http://docs.python.org/3.3/library/tempfile.html#tempfile.NamedTemporaryFile
>
>
>
> ChrisA
Thank you, Chris! I got it working and am going to take your advice on the
tempfile idea. I actually ran into the problem you were referring to, and kept
the .ahk files backed up elsewhere for when this situation arose. I appreciate
the help!
--
http://mail.python.org/mailman/listinfo/python-list
