Click here to Skip to main content
14,691,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read in an ascii delimited text file that has been parsed, and the SPACE characters have been replaced with the ',' (comma) character. I next need to convert this information to two columns and save it back to another file that I can bring into Microsoft Word.

Below is a sample of the information from the delimited file. Any help you can give me is appreciated. P.S. I am using Python 3.8. I'm new to python programming, or can't you tell! -Thanks!


(Sample of flat file, one long line...):
Package,Version,------------------------,---------,backcall,0.2.0,beautifulsoup4,4.9.3,cachetools,4.1.1,certifi,2020.6.20,chardet,3.0.4,cloudpickle,1.6.0,colorama,

What I have tried:

import os

# Works so far!
os.chdir('C:/')
CurrentDIR = os.getcwd()

# Works so far!
MyFile = open('C:/List.csv', 'rt')
MyString = MyFile.read()
MyFile.close()

def remove(MyString): 
    return ",".join(MyString.split())

OutputFile = open(r'c://users//larry//Output.txt','w')
OutputFile.write(remove(MyString))
OutputFile.close()

		    
Posted
Updated 13-Oct-20 23:17pm
Comments
Sandeep Mewara 14-Oct-20 4:20am
   
What are there in two columns? I am not sure of the output you are trying.

Have a look at this if this is what you are looking for: https://python4astronomers.github.io/files/asciifiles.html
Larry0419 14-Oct-20 13:03pm
   
Sandeep:

Thank you for responding! I took a look at the github link, however, I think this solution is a little too high powered for what I am trying to do. All I need to do is open the file and at each and every-other ',' (comma) start a new line, and continue until there are no more lines in the file to process. I did find that I can bring the original file (acquired by using: pip list > list.txt). I did find I can bring the original txt file into Excel, formatting (Two Column) intact, but I really wanted to do it with Python (3.8.5). Maybe when I am more savvy... again, Thank You!
Sandeep Mewara 14-Oct-20 14:13pm
   
Ok. But, I am still not clear on what exactly your end result looks like. That could help people to respond.

1 solution

Use the string.replace() method to change the first comma to a space, or other control character to use as the column separator. I am not sure if you can import csv files direct into MS Word but it is worth checking first.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900