0

I need a comma seperated txt file with txt extension. "a,b,c"

I used csv.writer to create a csv file changed the extension. Another prog would not use/process the data. I tried "wb", "w."

F = open(Fn, 'w')
w = csv.writer(F)
w.writerow(sym)
F.close()

opened with notepad ---These are the complete files. Their file: created using their gui used three symbols

PDCO,ICUI,DVA

my file : created using python

PDCO,ICUI,DVA

Tested: open thier file- worked, opened my file - failed. Simple open and close with save in notepad. open my file-- worked

Works= 'PDCO,ICUI,DVA' 
Fails= 'PDCO,ICUI,DVA\r\r\n' 

Edit: writing txt file without Cvs writer.....

sym = ['MHS','MRK','AIG']

with open(r'C:\filename.txt', 'w') as F:    # also try 'w'
    for s in sym[:-1]:                      # separate all but the last
        F.write(s + ',')                    # symbols with commas
        F.write(sym[-1])                    # end with the last symbol
Merlin
  • 24,552
  • 41
  • 131
  • 206
  • 7
    So what exactly is your problem? I don't quite get it yet, please try to be more specific. – Sven Marnach Jan 27 '11 at 15:37
  • 3
    And what's the problem you're facing? .csv are .txt files, too (just different extension). – wkl Jan 27 '11 at 15:37
  • Prob, dont know...... I used the created file as source file instead of typing in the info. the other prog just ignored it. – Merlin Jan 27 '11 at 15:39
  • 2
    "just ignored it"? What can this possibly mean? – S.Lott Jan 27 '11 at 15:45
  • @lott. A gui allows me to enter in sample info or enter info then save as txt file. So, I save as text file. test using their txt file it works. Now, create my own txt file with python. The gui import of info just ignores my txt file. I dont know their "dialect" or method for saving txt files. But, would prefer not inputting a list, when I could create it with python and use it instead – Merlin Jan 27 '11 at 15:53
  • 3
    @user428862. "ignores"? Didn't upload? Didn't process? Didn't provide an error messages? Crashed your browser? Crashed your PC? Crashed the server? "ignores" isn't a useful word. – S.Lott Jan 27 '11 at 16:09
  • @lott "Didn't upload? Didn't process?" I took python created file. Opened it in notepad, saved it. The third party gui read it. – Merlin Jan 27 '11 at 16:25
  • @user428862: Please post the full name (with extension) of "their txt file", and the first three lines of the file, and the full name (with extension) of "your txt file", and the first three lines of the file. – Hugh Bothwell Jan 27 '11 at 16:25
  • The file name isnt the issue both are ????.txt. the first three lines: how? thanks for help. – Merlin Jan 27 '11 at 16:30
  • @user428862: "the first three lines:"? You open the files. Both of them. You copy the first three lines of each file. You **update** the question with the first three lines of each file. When you're done, you'll have "post ... first three lines of the file" as requested. – S.Lott Jan 27 '11 at 16:35
  • @user428862: load the file in Notepad, select and copy the first three lines. Edit your question, at the bottom add 'their file: ' + paste and format as code (curly brackets in button bar at top of editor form). Repeat for your file. – Hugh Bothwell Jan 27 '11 at 16:36
  • @user428862: "the first three lines". Not the first line. The first 3 (three) lines. – S.Lott Jan 27 '11 at 17:55
  • thats all there is....those are the completed files... – Merlin Jan 27 '11 at 18:08
  • 1
    It's not clear to me that opening the files in notepad and pasting the contents is going to help. Obviously the difference between the files isn't showing up in notepad or the OP wouldn't need our help; probably notepad is silently changing the file in some way. My suggestion would be to open the files in a hex editor; but I don't know a windows hex editor. – senderle Jan 27 '11 at 18:30
  • it would be easier to programmatically 1) write file with python, 2)open and save with notepad to same location. – Merlin Jan 27 '11 at 18:56
  • @senderle: Notepad++ has a hex editor plugin. HxD is a good standalone hex editor. Just for future reference. – Soumya Jan 28 '11 at 17:34

5 Answers5

3

To me, it look like you don't exactly know you third party application input format. If a .CSV isn't reconized, it might be something else.

Did you try to change the delimiter fromn ';' to ','

import csv
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

Take a look in the CSV Python API

Philippe Lavoie
  • 2,583
  • 5
  • 25
  • 39
2

I think the problem is your file write mode, as per CSV file written with Python has blank lines between each row

If you create your csv file like

csv.writer(open('myfile.csv', 'w'))

csv.writer ends its lines in '\r\n', and Python's text file handling (on Windows machines) then converts '\n' to '\r\n', resulting in lines ending in '\r\r\n'. Many programs will choke on this; Notepad recognizes it as a problem and strips the extra '\r' out.

If you use

csv.writer(open('myfile.csv', 'wb'))

it produces the expected '\r\n' line ending, which should work as desired.

Edit: @senderle has a good point; try the following:

goodf = open('file_that_works.txt', 'rb')
print repr(goodf.read(100))
badf =  open('file_that_fails.txt', 'rb')
print repr(badf.read(100))

paste the results of that here, so we can see how the two compare byte-for-byte.

Community
  • 1
  • 1
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • I took python created file. Opened it in notepad, saved it. The third party gui read it. – Merlin Jan 27 '11 at 16:27
  • @user428862: DETAILS, already! File name? File extension? Changing the file extension fixed the problem? If you create the file with the proper extension in the first place, do you still need to resave it with Notepad? – Hugh Bothwell Jan 27 '11 at 16:30
  • Sorry.. No, same filename. same extension. just opened the file and closed, nothing else. The file name and extension is not the issue. my guess something how notepad saved vs python created ...something – Merlin Jan 27 '11 at 16:38
  • 1
    Ok! Now we're getting somewhere; so it is probably the file's text encoding. Are you using any odd character sets, unicode etc? Do you get any sort of error message loading the pre-Notepad file? – Hugh Bothwell Jan 27 '11 at 16:46
  • There is nothing odd about text. Stock symbols no numbers. I get no error messages. the fail is a silent fail. – Merlin Jan 27 '11 at 16:57
  • @ new idea, the first attempt was with 'wb', then 'w'. both had the same result. – Merlin Jan 27 '11 at 18:14
  • Works= 'PDCO,ICUI,DVA' Fails= 'PDCO,ICUI,DVA\r\r\n' Now the file was created with "w" not 'wb', Opening/Save files with 'wb' creation fails...... – Merlin Jan 27 '11 at 19:55
  • 1
    \r\r\n says that open(filename, 'w') is the problem, and open(filename, 'wb') ought to solve it. "fails" does not help; WHAT fails? Fails HOW? How do you know it failed? What did it do which it shouldn't, or not do that it should? Were there any error messages? – Hugh Bothwell Jan 28 '11 at 00:20
  • @Hugh Bothwell, good call, I didn't think of using python itself, silly of me! In any case, we now know that the \r\r\n is probably the problem. Could the problem be the "dialect=excel" keyword? – senderle Jan 28 '11 at 05:05
  • I keep saying their is no error msg. When it works, the data is accepted. When it fails, data is not accepted. Try again, data is not accepted. I call it a fail. it does not say fail, I call it a fail. – Merlin Jan 28 '11 at 13:23
  • @senderle. I dont use "dialect=excel." But, I recall the default is to "dialect=excel." maybe something here. – Merlin Jan 28 '11 at 13:25
  • @user428862: have you tried the `Sniffer` class? It looks at a sample and deduces the dialect for you. See here: http://docs.python.org/library/csv.html#csv.Dialect – senderle Jan 28 '11 at 16:58
  • Ran code below, it gave me nothing back....really starting to get annoyed import csv csvfile = open('C:\Fails.txt', "rb") dialect = csv.Sniffer().sniff(csvfile.read(100)) csvfile.seek(0) reader = csv.reader(csvfile, dialect) – Merlin Jan 28 '11 at 17:22
  • You're opening the "fails" file. You need to open the "works" file -- the point is that Sniffer reads the file that works and figures out the format it uses. I would guess that you want to read more than 100 bytes as well -- it's safe even if the file is shorter than that. See my answer below. – senderle Jan 28 '11 at 17:41
  • below gave mesame blank output could be user error. csvfile = open('C:\works.txt', "rb") dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) reader = csv.reader(csvfile, dialect) – Merlin Jan 28 '11 at 19:01
  • see my answer for code -- you're still not doing what I mean you to do. The `csvfile.seek(0)` and `reader` bit from the documentation example is useless to you. The point is to get the format by Sniffing, and then use that format to __write__, not to read. – senderle Jan 28 '11 at 19:30
1
  1. So, I save as text file.

  2. Now, create my own txt file with python.

What are the exact differences between their file and your file? Exact.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
1

I suspect that @Hugh's comment is correct that it's an encoding issue.

When you do a Save As in notepad, what's selected in the Encoding dropdown? If you select different encodings do some or all of those fail to be opened by the 3rd party program?

Davy8
  • 30,868
  • 25
  • 115
  • 173
  • If you select a different encoding to save as in Notepad does it stop working? If so your 3rd party program doesn't know how to properly handle the encoding that the CSV writer is setting (most likely either UTF8 or some other form of unicode). If no non-ANSI characters are used then text will look identical to a program that understands the encodings, but even though the text is the same that doesn't mean the file is the same. – Davy8 Jan 27 '11 at 18:47
  • Read http://www.joelonsoftware.com/articles/Unicode.html to at least have a basic understanding of what character encoding means. Text isn't just text. Most modern day programs should ideally understand unicode but some don't take the time to support it properly, and it seems like your 3rd party app is one of them. There may be a setting for the CSV exporter to export in ANSI/ASCII which should solve your problem, but really it's the 3rd party program, that's broken by modern standards. – Davy8 Jan 27 '11 at 18:50
1

Try this:

with open('file_that_works.csv', 'rb') as testfile:     # file is automatically
    d = csv.Sniffer().sniff(testfile.read(1024))        # closed at end of with
                                                        # block
with open(Fn, 'wb') as F:       # also try 'w'
    w = csv.writer(F, dialect=d)
    w.writerow(sym)

To explain further: this looks at a sample of a working .csv file and deduces its format. Then it uses that format to write a new .csv file that, hopefully, will not have to be resaved in notepad.


Edit: if the program you're using doesn't accept multi-line input (?!) then don't use csv. Just do something like this:

syms = ['JAGHS','GJKDGJ','GJDFAJ']
with open('filename.txt', 'wb') as F:       
    for s in syms[:-1]:                     # separate all but the last
        F.write(s + ',')                    # symbols with commas
    F.write(syms[-1])                       # end with the last symbol

Or more tersely:

with open('filename.txt', 'wb') as F:
    F.write(','.join(syms))

Also, check different file extensions (i.e. .txt, .csv, etc) to make sure that's not the problem. If this program chokes on a newline, then anything is possible.

senderle
  • 145,869
  • 36
  • 209
  • 233
  • :-(. still didnt work. Then I took the txt created by third party, use it as "Sniffer" file. ran it in above code. still didnt work...... "b" does not work. damn it... at complete loss. – Merlin Jan 28 '11 at 21:29
  • Ok, well we're working through possibilities. Next thought: I notice that `Works= 'PDCO,ICUI,DVA'` doesn't have a line terminator at all. What happens when the working file has more than one line? Create a working 2-line file, and show the output of `goodf = open('file_that_works.txt', 'rb'); print repr(goodf.read(100))` – senderle Jan 28 '11 at 22:49
  • thought.... since Works= 'PDCO,ICUI,DVA' does not have line terminator, How can create a txt file without one. The third party Works= 'PDCO,ICUI,DVA' file also may not have or need a second line. My reasoning is that the third party gui, 1) asks or symbols, 2) then click a save botton to create txt file. there is no second line. Nothings says to me that the third party file is well formed. thanks for ur help. – Merlin Jan 30 '11 at 15:42
  • Are you saying that the third party app can't generate a second line? What is this program?? Perhaps it can't handle multi-line input. That would be super bizarre; and frankly in that case I can't imagine why you'd bother to use csv.writer at all. Just spew a bunch of symbols separated by commas. See above. – senderle Jan 30 '11 at 15:57
  • thanks for edit. The edit actually worked! but giving me strange symbols combining some....???? see my edit above Syms giving me MHS,AIGMRK, AIG for MHS, AIGMRK,AIG. – Merlin Jan 31 '11 at 04:59
  • Python uses indentation to denote blocks. In my code, the for loop iterates over `write(s + ',')` multiple times, but the final line, `write(syms[-1])` is only executed once. But when you copied my code, you didn't preserve the indentation. Somehow the last line got indented another level, and so the for loop iterates over both lines, so each time it writes, it appends the last item in the list -- AIG. Make sense? – senderle Jan 31 '11 at 07:38
  • Better to just use the 2-line `','.join()` version anyway. – senderle Jan 31 '11 at 08:42