-2

I wrote this program to read a text file and put the contents line by line into another cvs file for writing. However, I would like to open the first txt file for reading, open another txt file for writing, and copy the contents of txt file 1 into txt file 2 without commas. For example, a line "1, 2, hello" should read "1 2 hello". I currently have it opening a csv file for writing, but I would like it to open a txt file for writing.

import csv

with open('texfile.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split(",") for line in stripped if line)
    with open('csvfile.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerows(lines)
J.D.
  • 1
  • 1
  • 2
  • You can look at the source for the csv module and see what it does. – Ken White Mar 27 '17 at 17:50
  • Why don't you want to import the `csv` module? You can't use a `csv.writer` without importing the module – Ben Mar 27 '17 at 17:51
  • "I would like to do this without importing the CSV function in my program" - That is a bad idea. – TigerhawkT3 Mar 27 '17 at 17:53
  • @KenWhite fixed question. I meant I'm currently opening it into a CSV file for writing but would like to do the same thing except open it in a txt file. – J.D. Mar 27 '17 at 17:54
  • Do you mean you want the CSV file to open in a text editor? – TigerhawkT3 Mar 27 '17 at 17:54
  • Or do you want to simply remove all commas from an ordinary text file? Is it supposed to be a CSV at all? – TigerhawkT3 Mar 27 '17 at 17:57
  • @TigerhawkT3 I want to open the first txt file for reading, and read in each line to another txt file for writing without commas. For example, a line "1, 2, hello" should read "1 2 hello". I currently have it opening a csv file for writing – J.D. Mar 27 '17 at 17:57
  • It sounds like your question has literally nothing to do with the CSV format and all you want to do is remove all of a certain character from a file. – TigerhawkT3 Mar 27 '17 at 17:58
  • JD wants to take a TXT file in which has commas, remove the commas and save the file as .csv but really with the space delimiter. – Garren S Mar 27 '17 at 17:59
  • @Garren - Do they? It's totally unclear. – TigerhawkT3 Mar 27 '17 at 18:00
  • @TigerhawkT3 Fixed the question does this make more sense? – J.D. Mar 27 '17 at 18:02
  • Looks like my suspicions were correct; this has nothing at all to do with CSV and all you want to do is remove all instances of a certain character from a file. – TigerhawkT3 Mar 27 '17 at 18:03

1 Answers1

0

Assuming your input txt file is safe to parse via the simple ".split()" function (as you've specified), writing to a basic CSV file should be straightforward:

with open('csvfile.csv', 'w') as out_file:
  for line in lines:
    out_file.write(" ".join(line) + "\n")

NOTE: Using the CSV module would still be best.

Garren S
  • 5,552
  • 3
  • 30
  • 45
  • 1
    This will add extra spaces. I think OP might simply want `with open(a) as i, open(b, 'w') as o: b.write(a.read().replace(',', ''))`. – TigerhawkT3 Mar 27 '17 at 18:00