LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2012, 03:15 PM   #1
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
python, trying to write multiple variables to a file on single line


Trying to capture 2 variables from user input, then write them to a file on the same line.


Code:
#!/usr/local/bin/python
# Get raw input from command line.
word1 = raw_input("Pick a noun: ")
word2 = raw_input("Describe it: ")
#Open txt file for writing
outfile = open('/python/EXAMPLES/simpletext.txt','w')

outfile.write('Running script\n')
outfile.write(word1, word2)
Quote:
$ ./open-write-to-file.py
Pick a noun: cat
Describe it: is cool
Traceback (most recent call last):
File "./open-write-to-file.py", line 9, in <module>
outfile.write(word1, word2)
TypeError: function takes exactly 1 argument (2 given)

outfile.write('Running script\n')
words = word1 + word2
outfile.write(words)


Is as close as I got but the formatting isn't right.

Would also like a space between selection, and a newline at the end.


Thanks
 
Old 02-04-2012, 03:19 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Code:
outfile.write('{0} {1}\n'.format(word1, word2))
 
Old 02-04-2012, 03:24 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
dugan's approach should work. Also, you can use good ol' print:

For python pre-3.0:
Code:
print >> outfile, word1, word2
For python post-3.0:
Code:
print ( word1, word2, file=outfile )
 
Old 02-04-2012, 03:29 PM   #4
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35
Prefect, could you explain what that part is doing?

Are the 0 and 1 the positional locations of the 2 variables? Does it change it to a single string or something?
 
Old 02-04-2012, 03:38 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Not to steal dugan's thunder... but I noticed he went offline.

Quote:
Are the 0 and 1 the positional locations of the 2 variables? Does it change it to a single string or something?
Yes and yes.

EDIT:
More specifically, the format() method returns a copy of the string with the respective values inserted. There are other ways to describe how variables are to be substituted. You can refer to them by name:
Code:
vars['word1'] = raw_input("Pick a noun: ")
vars['word2'] = raw_input("Describe it: ")

...

outfile.write('{word1} {word2}\n'.format( **vars ) )
There are also ways to explicitly tell python that the associated value should be treated as a numeric value as opposed to a string, left- or right-aligned, zero- or space-padded, etc.

Last edited by Dark_Helmet; 02-04-2012 at 03:43 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
how to write command line arguments as values, into a file which contains variables sathiawathi.m Linux - Newbie 2 07-17-2009 02:27 AM
writting multiple variables to a single file knobby67 Programming 5 06-23-2009 01:10 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration