Python ? Attempting to format new text file

1599
11
Jump to solution
12-06-2016 11:52 AM
MichaelMiller2
Occasional Contributor III

I'm using the below code and would like to write the output to a new file and write it all to a single line formatted as such:

'D1_06689','D1_0670',......

infile2 = open(r'C:\tmp\RampWork\OutFile.txt', 'r')
outfile2 = open(r'C:\tmp\RampWork\OutFile2.txt', 'w')

for f in infile2:
     outfile2.write("'%s'," % (f))

infile2.close()
infile2.close()

infile2:

D1_06689
D1_06705
D1_06706
D1_06707
D1_06729
D1_06730
D1_06731
D1_06732
D1_06733
D1_06734
D1_07043
D1_07044
D1_07045
D1_07046
D1_07047
D1_07048
D1_07122
D1_07123

outfile2:

'D1_06689
','D1_06705
','D1_06706
','D1_06707
','D1_06729
','D1_06730
','D1_06731
','D1_06732
','D1_06733
','D1_06734
','D1_07043
','D1_07044
','D1_07045
','D1_07046
','D1_07047
','D1_07048
','D1_07122
','D1_07123
','D1_07124
','D1_07125
','D1_07126
','D1_07412
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MichaelMiller2
Occasional Contributor III

I think I solved it. Changed the write mode to binary and it removed the newlines

Thnaks to this thread on StackOverflow http://stackoverflow.com/questions/4025760/python-file-write-creating-extra-carriage-return

import os
mstring = ''

infile = open(r'C:\tmp\RampWork\OutOfCityLimits2.txt', 'r')
outfile = open(r'C:\tmp\RampWork\OutFile.txt', 'wb')

for f in infile:
     mstring += ("'%s'""," % (f))
     #print mstring

outfile.write(mstring)

infile.close()
outfile.close()

View solution in original post

0 Kudos
11 Replies
DarrenWiens2
MVP Honored Contributor

I suppose rather than calling write within the loop, build your master string (using +=) within the loop and then call write once outside.

0 Kudos
MichaelMiller2
Occasional Contributor III

Good idea, thanks Darren. But it still is using a carriage return after each f, even in the master string. I can't for the life of me figure out why that would be.

0 Kudos
MichaelMiller2
Occasional Contributor III

I think I solved it. Changed the write mode to binary and it removed the newlines

Thnaks to this thread on StackOverflow http://stackoverflow.com/questions/4025760/python-file-write-creating-extra-carriage-return

import os
mstring = ''

infile = open(r'C:\tmp\RampWork\OutOfCityLimits2.txt', 'r')
outfile = open(r'C:\tmp\RampWork\OutFile.txt', 'wb')

for f in infile:
     mstring += ("'%s'""," % (f))
     #print mstring

outfile.write(mstring)

infile.close()
outfile.close()
0 Kudos
DanPatterson_Retired
MVP Emeritus

For future/imminent proofing... It is documented as well 16.1. os — Miscellaneous operating system interfaces — Python 3.5.2 documentation 

open (file ) the U option is deprecated in 3.5 2. Built-in Functions — Python 3.5.2 documentation but other incarnations including 11.1. pathlib — Object-oriented filesystem paths — Python 3.5.2 documentation  with read_text and write_text plus other options exist

MichaelMiller2
Occasional Contributor III

I may have jumped the gun on my solution. the 'wb' is not currently working. I still have '\n''s in my output.

0 Kudos
DanPatterson_Retired
MVP Emeritus

you would have to write it without the \n in the first place or strip them after

txt = list('abcde')
>>> txt_file = "\n".join([i for i in txt])
>>> txt_file
'a\nb\nc\nd\ne'
>>> print(txt_file)
a
b
c
d
e
>>> in_file = txt_file.replace("\n"," ")
>>> in_file
'a b c d e'
MichaelMiller2
Occasional Contributor III

Dan,

Thanks for the pointers. It's working, but I need to add single quotes to each value in the output separated by commas. Could use a pointer on how I go about formatting that.  

import os
mstring = ''

infile = open(r'C:\tmp\RampWork\OutOfCityLimits2.txt', 'r')
outfile = open(r'C:\tmp\RampWork\OutFile.txt', 'w')


#mstring = ''.join("'{}'".format([f for f in infile]))
mstring = ''.join([f for f in infile])

#print (mstring)

#for f in infile:
     #mstring = 
     #mstring += "'{}'{}".format(f, ',')
     #print mstring

mstring2 = mstring.replace('\n',',')
#mstring2 = '{}{}'.format(mstring.replace('\n',''), ',')
print mstring2
outfile.write(mstring2)

infile.close()
outfile.close()
0 Kudos
DanPatterson_Retired
MVP Emeritus
>>> txt = list('abcde')
>>> txt_file = "\n".join(["'{}'".format(i) for i in txt])
>>> txt_file
"'a'\n'b'\n'c'\n'd'\n'e'"
>>> print(txt_file)
'a'
'b'
'c'
'd'
'e'
>>> ‍‍‍‍‍‍‍‍‍‍‍

Like that???

0 Kudos
MichaelMiller2
Occasional Contributor III

Getting closer.  What i'm shooting for is to dump output as a single line with values enclosed in single quotes separated by commas. I will need to insert the single quotes after the '\n' have been removed. Line 22 is what I'm having an issue with getting the syntax correct.

import os
mstring = ''

infile = open(r'C:\tmp\RampWork\OutOfCityLimits2.txt', 'r')
outfile = open(r'C:\tmp\RampWork\OutFile.txt', 'wb')


#mstring = ''.join(["'{}'".format(f) for f in infile])
mstring = ''.join([f for f in infile])

print (mstring)

#for f in infile:
     #mstring = 
     #mstring += "'{}'{}".format(f, ',')
     #print mstring

mstring2 = mstring.replace('\n','')
#mstring2 = '{}{}'.format(mstring.replace('\n',''), ',')
print mstring2

mstring3 = "'{}'{}".format(f) for f in mstring2,','
print mstring3

outfile.write(mstring3)

infile.close()
outfile.close()
0 Kudos