Select to view content in your preferred language

Python ? Attempting to format new text file

1777
11
Jump to solution
12-06-2016 11:52 AM
MichaelMiller2
Regular Contributor

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
11 Replies
DanPatterson_Retired
MVP Emeritus

Last kick...

>>> txt = list('abcde')
>>> txt_file = "".join(["'{}', ".format(i) for i in txt])
>>> print(txt_file)
'a', 'b', 'c', 'd', 'e', 
>>> 
MichaelMiller2
Regular Contributor

Got it.  Thank you very much @Dan Patterson

0 Kudos