Data Manipulation

This is the place for queries that don't fit in any of the other categories.

Data Manipulation

Postby brianvt » Thu May 30, 2013 8:35 pm

I am trying to massage some large data files containing records in order to import them into Access.
The following code shows my problem:
Code: Select all
myfile = open('test.txt','w')
n2 = 'John'
myfile.write(n2)
myfile.write('n1')
myfile = open('test.txt','r')
print myfile.readline()


gives me: Johnn1

where I want: 'John', 'n1'

Any help will be appreciated.
Last edited by stranac on Thu May 30, 2013 8:45 pm, edited 1 time in total.
Reason: Added code tags
brianvt
 
Posts: 4
Joined: Thu May 30, 2013 7:13 pm

Re: Data Manipulation

Postby metulburr » Thu May 30, 2013 8:57 pm

gives me: Johnn1

where I want: 'John', 'n1'

you get that output because that is what you are writing to the file:
if you want the apostraphe's of the string also written
Code: Select all
repr(n2)
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1130
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Data Manipulation

Postby brianvt » Fri May 31, 2013 7:43 am

Thanks for the help. Is there an easy way to keep the strings separate?
brianvt
 
Posts: 4
Joined: Thu May 30, 2013 7:13 pm

Re: Data Manipulation

Postby stranac » Fri May 31, 2013 8:13 am

Yes, write the thing you want to separate them in between. Something like this should work:
Code: Select all
myfile.write("{!r}, 'n1'".format(n2))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 911
Joined: Thu Feb 07, 2013 3:42 pm

Re: Data Manipulation

Postby brianvt » Fri May 31, 2013 10:30 am

Thanks for your help, I tried your suggestion, namely:

myfile.write("{!r}, 'n1' ", format(n2))

but it produces the following error:
Traceback (most recent call last):
File "C:\Users\Brian\Music\rubbish", line 4, in <module>
myfile.write("{!r}, 'n1' ", format(n2))
TypeError: function takes exactly 1 argument (2 given)
brianvt
 
Posts: 4
Joined: Thu May 30, 2013 7:13 pm

Re: Data Manipulation

Postby micseydel » Fri May 31, 2013 10:36 am

Look at his and yours. They're not the same. His would work.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Data Manipulation

Postby brianvt » Fri May 31, 2013 12:01 pm

Oops!!!
Sorry about that.
brianvt
 
Posts: 4
Joined: Thu May 30, 2013 7:13 pm

Re: Data Manipulation

Postby MichelFJM » Mon Jun 03, 2013 2:00 pm

Hello

You can also write it like this (that is add yourself the ') :
Code: Select all
myfile = open('test.txt','w')
n2 = 'John'
myfile.write("'"+n2+"', 'n1' \n")
myfile.close()
print open('test.txt','r').readline()
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests