Python question

I am trying to write a program in python, of which I am pretty new. The program is supposed to ask the user for a list of names that will be coming from a spreadsheet, there are multiple names per cell separated by commas. After the names are entered the program will format them for use in other programs. The problem I am having is that when you copy and paste the cells that you want, it is like it is pasting it then automatically entering the data, then it somehow is pasting each line to the regular terminal prompt and automatically entering it.

This is what I have done so far:

names = raw_input('Shoot me some names partner: ')

print ‘What do you want to do?’
print ‘1 - format names for program 1’
print ‘2 - format names for program 2’

first_act = raw_input('Enter choice: ')

print names
print first_act

Now when I run this and input the dummy names I put into a google doc spreadsheet to test, as soon as I hit shift+ctl+v, without hitting enter I get this:

seth@linux-1337:~> python pythonproj/names.py
Shoot me some names partner: abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnopWhat do you want to do?
1 - format names for program 1
2 - format names for program 2
Enter choice: abcd,efg,hijkl,mnop
abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop
If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop
If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop
If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop
If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop
If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf abcd,efg,hijkl,mnop
seth@linux-1337:~> abcd,efg,hijkl,mnop

Any idea what could be causing this and what I can to to change it? This is python 2.7.

On Wed, 18 Jan 2012, hito kiri wrote:

>
> I am trying to write a program in python, of which I am pretty new. The
> program is supposed to ask the user for a list of names that will be
> coming from a spreadsheet, there are multiple names per cell separated
> by commas. After the names are entered the program will format them for
> use in other programs. The problem I am having is that when you copy and
> paste the cells that you want, it is like it is pasting it then
> automatically entering the data, then it somehow is pasting each line to
> the regular terminal prompt and automatically entering it.
>
> This is what I have done so far:
>
> names = raw_input('Shoot me some names partner: ')
>
> print ‘What do you want to do?’
> print ‘1 - format names for program 1’
> print ‘2 - format names for program 2’
>
> first_act = raw_input('Enter choice: ')
>
> print names
> print first_act
>
> Now when I run this and input the dummy names I put into a google doc
> spreadsheet to test, as soon as I hit shift+ctl+v, without hitting enter
> I get this:
>
> seth@linux-1337:~> python pythonproj/names.py
> Shoot me some names partner: abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnopWhat do you want to do?
> 1 - format names for program 1
> 2 - format names for program 2
> Enter choice: abcd,efg,hijkl,mnop
> abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
> If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to
> lookup the package that contains it, like this:
> cnf abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
> If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to
> lookup the package that contains it, like this:
> cnf abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
> If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to
> lookup the package that contains it, like this:
> cnf abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
> If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to
> lookup the package that contains it, like this:
> cnf abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
> If ‘abcd,efg,hijkl,mnop’ is not a typo you can use command-not-found to
> lookup the package that contains it, like this:
> cnf abcd,efg,hijkl,mnop
> seth@linux-1337:~> abcd,efg,hijkl,mnop
>
>
> Any idea what could be causing this and what I can to to change it?
> This is python 2.7.
>
>
> –
> hito_kiri
> ------------------------------------------------------------------------
> hito_kiri’s Profile: http://forums.opensuse.org/member.php?userid=26350
> View this thread: http://forums.opensuse.org/showthread.php?t=471329
>
>

Hi,

If you want to get the data from a file, it would be easier to read the data in directly from the file rather than pasting it in.

In python you can do:

for line in open(“FILENAME”):

to loop through each line of a file which can then be processed as required.

I’m guessing your pasted input is several lines long and contains newline characters which are acting like the return key.

Kind regards,
Barry D. Nichols

Ahh, I assumed that there were perhaps newline characters and that was causing this, but I was not sure. Unfortunately because of how this will be used, it needs to be able to be copied and pasted. Is there a way to tell raw_input to ignore newline characters?

Hi,

Could you put it in a loop which exits when the only input is the return key?

Kind regards,
Barry D. Nichols

That sounds like about what I am after, but could you give an example of how I would do that with raw_input? I am not very experienced with programming in general, and I am pretty new to python. I have wrote loops for other things, but never using raw_input, I am having a hard time thinking how that would be done.

On Thu, 19 Jan 2012, hito kiri wrote:

>
> That sounds like about what I am after, but could you give an example of
> how I would do that with raw_input? I am not very experienced with
> programming in general, and I am pretty new to python. I have wrote
> loops for other things, but never using raw_input, I am having a hard
> time thinking how that would be done.

A basic example might be:

allnames = ]
name = " "
while name != “”:
name = raw_input('Enter some names: ')
if name != “”:
allnames.append(name)

Which would keep prompting for names and adding them to ‘allnames’ until an empty string is the input, assuming you want them in a list. Also, you could split the names from each line and store them as seperate items in the list.

Kind regards,
Barry D. Nichols

Python has a csv library -

13.1. csv — CSV File Reading and Writing Python v2.7.2 documentation

Hi,

OP already ruled out reading in the file “Unfortunately because of how this will be used, it needs to be able to be copied and pasted”.

Kind regards,
Barry D. Nichols

Am 20.01.2012 15:36, schrieb Barry Nichols:
>
> Hi,
>
> mchnz;2431915 Wrote:
>> Python has a csv library -
>>
>> ‘13.1. csv — CSV File Reading and Writing Python v2.7.2
>> documentation’ (http://docs.python.org/library/csv.html)
>
> OP already ruled out reading in the file “Unfortunately because of how
> this will be used, it needs to be able to be copied and pasted”.
>
It should be possible to pass the standard input stream to the csv
reader, it does not need to be a file on the hard disk.


PC: oS 11.4 (dual boot 12.1) 64 bit | Intel Core i7-2600@3.40GHz | KDE
4.6.0 | GeForce GT 420 | 16GB Ram
Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.7.4 |
nVidia ION | 3GB Ram

Am 20.01.2012 15:50, schrieb Martin Helm:
> It should be possible to pass the standard input stream to the csv
> reader, it does not need to be a file on the hard disk.
>
Yes that works

>>> import sys
>>> import csv
>>> reader = csv.reader(sys.stdin, delimiter=",", quotechar="’")
>>> for row in reader:
… print row

After you copied and pasted press [CTRL]+D to signal end of file for the
input.


PC: oS 11.4 (dual boot 12.1) 64 bit | Intel Core i7-2600@3.40GHz | KDE
4.6.0 | GeForce GT 420 | 16GB Ram
Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.7.4 |
nVidia ION | 3GB Ram

Sorry that my previous post did not include code tags, now it is to late to change it. Hope the idea is clear.