6

I want to convert a couple of .csv files to .txt files using python. In my .csv files, I have hundreds of lines of data like the bellow: image of the csv file

Value   Date    Time
919     4/15/2016   19:41:02
551     4/15/2016   19:46:51
717     4/15/2016   19:49:48
2679    4/15/2016   19:52:49
2890    4/15/2016   19:55:43
2897    4/15/2016   19:58:38
1790    4/15/2016   21:39:14
2953    4/15/2016   21:42:10
2516    4/15/2016   21:45:04
2530    4/15/2016   21:47:58
2951    4/15/2016   21:51:02
2954    4/15/2016   21:53:56
2537    4/15/2016   21:56:52
2523    4/15/2016   21:59:45
2536    4/15/2016   22:02:49
2727    4/15/2016   22:05:43

I use the bellow code for this purpose.

csv_file = input('Enter the name of your input file: ')
txt_file = input('Enter the name of your output file: ')

text_list = []

with open(csv_file, "r") as my_input_file:
    for line in my_input_file:
        line = line.split(",", 2)
        text_list.append(" ".join(line))

with open(txt_file, "w") as my_output_file:
    my_output_file.write("#1\n")
    my_output_file.write("double({},{})\n".format(len(text_list), 2))
    for line in text_list:
        my_output_file.write("  " + line)
    print('File Successfully written.')

My first problem is that when the name of the input file is (for example) "DFW002_0330PM_Thursday_November_16_2017", I get the bellow error:

Traceback (most recent call last):
  File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in <module>
    csv_file = input('Enter the name of your input file: ')
  File "<string>", line 1, in <module>
NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined

But, when I change the name of the code to (for example) "11", the code defines the file and goes to the next steps, but again it returns the bellow error:

Traceback (most recent call last):
  File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in <module>
    with open(csv_file, "r") as my_input_file:
TypeError: coercing to Unicode: need string or buffer, int found

Would you please help me handle these problems?

B_R
  • 99
  • 1
  • 1
  • 7
  • why does your example not have commas? is that on purpose or mistake. It cant be a csv without commas correct? what am I missing here? – PYA Nov 16 '17 at 21:53
  • I copied this data from the csv file and copied here, maybe because of that, it is shown like this. If I can upload the file or an image of that you can see that. – B_R Nov 16 '17 at 21:57
  • 1
    which line gives you the `TypeError` – PYA Nov 16 '17 at 21:59
  • Please post the complete Traceback formatted as code. – wwii Nov 16 '17 at 22:00
  • line 6, in with open(csv_file, "r") as my_input_file: – B_R Nov 16 '17 at 22:01
  • also, looks like you are simply copying the files with a .txt extension. you can just do something like `for file in ./*.csv; do cp $file ./output/$file; done` if on linux – PYA Nov 16 '17 at 22:02
  • @B_R for that error try this `csv_file =str(input('Enter the name of your output file: '))` convert your input to a `string` and maybe that will help. – PYA Nov 16 '17 at 22:03
  • 1
    Python 2.x: `input()` is trying to evaluate your *input* that is why you are getting a NameError. Use `raw_input()` instead. This is a duplicate question. – wwii Nov 16 '17 at 22:08
  • Actually I tried csv_file =str(input('Enter the name of your output file: ')), but got the same error! – B_R Nov 16 '17 at 22:35
  • see my solution below – loretoparisi Nov 16 '17 at 22:35

1 Answers1

20

Using csv it's very easy to iterate over the csv lines:

import csv
csv_file = raw_input('Enter the name of your input file: ')
txt_file = raw_input('Enter the name of your output file: ')
with open(txt_file, "w") as my_output_file:
    with open(csv_file, "r") as my_input_file:
        [ my_output_file.write(" ".join(row)+'\n') for row in csv.reader(my_input_file)]
    my_output_file.close()
loretoparisi
  • 15,724
  • 11
  • 102
  • 146