5

I wish to write a simple text file with 6 lines in Python 2.7. I'm using this code:

import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter=' ', quoting=csv.QUOTE_NONE)
writer.writerow('0.06')
writer.writerow('0.00')
writer.writerow('0.00')
writer.writerow('-0.06')
writer.writerow('-10.59')
writer.writerow('38.49')
export.close()

And I'm getting inside the file:

0 . 0 6
0 . 0 0
0 . 0 0
- 0 . 0 6
- 1 0 . 5 9
3 8 . 4 9

But I don't want spaces or anything else inside the numbers, I need simply this:

0.06
0.00
0.00
-0.06
-10.59
38.49

But when I try delimiter='' or delimiter=None, I get the error "delimiter must be set". How can I write my numbers without delimiters? Maybe it is a very basic question but I can't find it in google. Thank you!

nadya
  • 240
  • 1
  • 2
  • 10

5 Answers5

8

writerow expects an iterable, each element of which will be written to the file, separated by the delimiter. Hence when you give it a string (which itself is an iterable), it writes each character to the file, separated by the delimiter.
What you want to do instead, is to supply the "row" as a list of strings. In your case, each row has only one string, so supply each row as a list with only one string.

The CSV format requires a delimiter of some sort. Classically, this delimiter is the comma - hence the name (CSV = Comma Separated Values). However, should you feel the need to use a different delimiter, you could of course do so (typical choices include space, tab, hyphens, etc)

import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter=',', quoting=csv.QUOTE_NONE)
writer.writerow(['0.06'])
writer.writerow(['0.00'])
writer.writerow(['0.00'])
writer.writerow(['-0.06'])
writer.writerow(['-10.59'])
writer.writerow(['38.49'])
export.close()
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 2
    To expand on this, `'0.06'` is a string, which is a sequence of strings. Therefore each item is written with a delimiter in between. By wrapping the string `'0.06'` in brackets `[]`, you create a list of a single string, so the whole string prints. – Bert Jan 16 '14 at 21:44
  • 2
    Creating `list` objects just to use the CSV module is masking the fact that the CSV module is the wrong tool for the job; analogous to wrapping a sledgehammer in bubble-wrap to avoid breaking the nut you're cracking. – johnsyweb Jan 16 '14 at 21:53
  • True. I figured OP had a reason for using CSV that I didn't know about. It wasn't until later that OP edited into his post that this was the only way he knew to write to a file. By then of course, someone else had already suggested `fp.write(...)`; so I wasn't going to mention that again – inspectorG4dget Jan 16 '14 at 22:17
4

As it has been said in the comments, you don't even need to use the csv module. A more simple solution:

with open('./images2/test.tfw', 'w') as export:
    export.write('0.06\n')
    export.write('0.00\n')
    export.write('0.00\n')
    export.write('-0.06\n')
    export.write('-10.59\n')
    export.write('38.49')
eskaev
  • 1,108
  • 6
  • 11
2

"No delimiter", you say? Other than the new-line, I presume. This is just writing lines of text in Python...

data = (0.06, 0.00, 0.00, -0.06, -10.59, 38.49,)
with open('./images2/test.tfw', 'w') as export:
    for line in data:
        export.write('{}\n'.format(line))
Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1
import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter='', quoting=csv.QUOTE_NONE)
writer.writerow(['0.06'])
writer.writerow(['0.00'])
writer.writerow(['0.00'])
writer.writerow(['-0.06'])
writer.writerow(['-10.59'])
writer.writerow(['38.49'])
export.close()

I've not worked much with csvwriter, but providing nothing between quotes as above, or perhaps NUL without quotes may do the trick? This is considering you want 0 delimitation in your CSV.

If your looking to simply write text, the following may suit your purposes. No need for csvwriter if your not looking to use CSV delimitation.

a = open('./images2/test.tfw', "w")
a.write('0.06\n')
a.write'0.00\n')
a.write('0.00\n')
a.write('-0.06\n')
a.write('-10.59\n')
a.write('38.49\n')
a.close()
0xhughes
  • 2,703
  • 4
  • 26
  • 38
  • Thank you, the second method is working, but the first (as I said in the question) gives me the error "delimiter must be set". – nadya Jan 16 '14 at 21:53
0

You could do it like this to get around the problem that strings are sequences:

import csv

def write_string(writer, s):
    return writer.writerow((s,))

with open('test.tfw', "wb") as export:
    writer = csv.writer(export, quoting=csv.QUOTE_NONE)
    write_string(writer, '0.06')
    write_string(writer, '0.00')
    write_string(writer, '0.00')
    write_string(writer, '-0.06')
    write_string(writer, '-10.59')
    write_string(writer, '38.49')
martineau
  • 119,623
  • 25
  • 170
  • 301