Re: Reading, Modifying, and Writing a file with data with quotation marks
- To: mathgroup at smc.vnet.net
- Subject: [mg29419] Re: Reading, Modifying, and Writing a file with data with quotation marks
- From: Tom Burton <tburton at cts.com>
- Date: Tue, 19 Jun 2001 05:35:37 -0400 (EDT)
- References: <9gkb6n$dul$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hello, On Mon, 18 Jun 2001 07:39:03 +0000 (UTC), in comp.soft-sys.math.mathematica you wrote: >I would like to modify a data file on disk which has >the data enclosed in quotation marks. >A much-shortened example would be as follows... Given the following file, In[374]:= !!d:\local\mmatest\test2.txt "big","left","15","0","0","21.87","0","0","","0","smy" "82","back","up","24","0","0","0","50","0","0","0","0","984" "44","","up","symm" reading it is relatively easy, provided that the character "," does not appear in any string. Read the file as string-valued words, creating a separate list for each line: In[367]:= data = ReadList["d:\\local\\mmatest\\test.txt", Word, WordSeparators->{","}, RecordLists->True]; Modify the data: In[368]:= data2 = Insert[data,"\"0\"",{{1,4},{1,4},{2,5},{2,5},{2,5}}]; This next part is a bit messy. Perhaps someone will suggest a better way. Combine each line of strings into one string, inserting comma-strings between each word and appending a new-line character: In[369]:= data3 = StringDrop[StringJoin@@(# /. s_String:>StringJoin[s,","]), -1] <> "\n" & /@ data2; This form of the data is just right for WriteString: In[370]:= stream=OpenWrite["d:\\local\\mmatest\\test2.txt"]; WriteString[stream,Sequence@@data3] Close[stream]; Then In[373]:= !!d:\local\mmatest\test2.txt "big","left","15","0","0","21.87","0","0","","0","smy" "82","back","up","24","0","0","0","50","0","0","0","0","984" "44","","up","symm" If the separator "," appears some of the words, then you must work a bit harder. I have a function that can do that job. Let me know if you need it. Regards, Tom Burton