473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert a text file to csv

3 New Member
Hello All,

I have a text file in the following format
Expand|Select|Wrap|Line Numbers
  1. <
  2. line 1
  3. line 2
  4. line 3
  5. line < 4
  6. line > 5
  7. >
  8. <
  9. line 1
  10. line < 2
  11. line >3
  12. >
  13.  
and so on

I need to convert it to csv so it looks like this:

Expand|Select|Wrap|Line Numbers
  1. <,line 1,line 2,line 3,line < 4,line > 5,>
  2. <,line 1,line < 2,line >3,>
  3.  
So basically, in pseudo code, I need to do the following:

find opening marker ( start of line = < )
join each subsequent line with a comma delimiter
find ending marker ( start of line = > )
continue thru file

Any helpers would be gratefully received

Thanks
Apr 25 '07 #1
9 12611
bartonc
6,596 Recognized Expert Expert
Hello All,

I have a text file in the following format
Expand|Select|Wrap|Line Numbers
  1. <
  2. line 1
  3. line 2
  4. line 3
  5. line < 4
  6. line > 5
  7. >
  8. <
  9. line 1
  10. line < 2
  11. line >3
  12. >
  13.  
and so on

I need to convert it to csv so it looks like this:

Expand|Select|Wrap|Line Numbers
  1. <,line 1,line 2,line 3,line < 4,line > 5,>
  2. <,line 1,line < 2,line >3,>
  3.  
So basically, in pseudo code, I need to do the following:

find opening marker ( start of line = < )
join each subsequent line with a comma delimiter
find ending marker ( start of line = > )
continue thru file

Any helpers would be gratefully received

Thanks
I'm just going to wing this (I'm not at a computer with Python installed):
Expand|Select|Wrap|Line Numbers
  1. theData = open(r"the\file\path\filename.txt")
  2. for line in theData:
  3.     marker = line[0]
  4.     if marker == "<":
  5.         while marker != ">":
  6.         nextLine = theData.next()
  7.         print nextLine
  8.         marker = nextLine[0]
Something like that, anyway. It shoud be enough to get you started.
Apr 25 '07 #2
olivercfc
3 New Member
Something like that, anyway. It shoud be enough to get you started.
thanks - I'll give it a whirl
Apr 25 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Hello All,

I have a text file in the following format
Expand|Select|Wrap|Line Numbers
  1. <
  2. line 1
  3. line 2
  4. line 3
  5. line < 4
  6. line > 5
  7. >
  8. <
  9. line 1
  10. line < 2
  11. line >3
  12. >
  13.  
and so on

I need to convert it to csv so it looks like this:

Expand|Select|Wrap|Line Numbers
  1. <,line 1,line 2,line 3,line < 4,line > 5,>
  2. <,line 1,line < 2,line >3,>
  3.  
So basically, in pseudo code, I need to do the following:

find opening marker ( start of line = < )
join each subsequent line with a comma delimiter
find ending marker ( start of line = > )
continue thru file

Any helpers would be gratefully received

Thanks
This uses string concatenation. It is not efficient, but it works:
Expand|Select|Wrap|Line Numbers
  1. fList = [s.strip() for s in open('text_to_csv.txt').readlines()]
  2.  
  3. outStr = ''
  4. for i in fList:
  5.     if i == '>':
  6.         outStr += (i+'\n')
  7.     else:
  8.         outStr += (i+',')
  9.  
  10. f = open('text_to_csv.csv', 'w')
  11. f.write(outStr.strip())
  12. f.close()
Apr 25 '07 #4
olivercfc
3 New Member
This uses string concatenation. It is not efficient, but it works
works perfectly for me - thanks!
Apr 25 '07 #5
bvdet
2,851 Recognized Expert Moderator Specialist
works perfectly for me - thanks!
What I meant by inefficient: String concatenation is somewhat inefficient compared to s.join() for example. After thinking about it, reading a file and writing to another file takes far longer to process compared to the string addition, so it just does not matter.
Apr 25 '07 #6
ghostdog74
511 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. data=open("file").read()
  2. pat = re.compile("^<$(.*?)^>$",re.M|re.DOTALL)
  3. for item in pat.findall(data):
  4.     item=item.replace("\n",",")
  5.     print "<",item,">"
  6.  
output:
Expand|Select|Wrap|Line Numbers
  1. # ./test.py
  2. < ,line 1,line 2,line 3,line < 4,line > 5, >
  3. < ,line 1,line < 2,line >3, >
  4.  
Apr 25 '07 #7
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. fList = open('text_data').read().split('>\n<')
  2. f = open('csv_data', 'w')
  3. f.write(',>\n'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList]))
  4. f.close()
So far we have three methods that do basically the same thing. Let's see what timeit shows us:
Expand|Select|Wrap|Line Numbers
  1. def test1(s):
  2.     fList = [i.strip() for i in s.split('\n')]
  3.     outStr = ''
  4.     for i in fList:
  5.         if i == '>':
  6.             outStr += (i+'\n')
  7.         else:
  8.             outStr += (i+',')
  9.     return outStr
  10.  
  11. def test4(s):
  12.     import re
  13.     pat = re.compile("^<$(.*?)^>$",re.M|re.DOTALL)
  14.     return ''.join(['%s%s%s' % ("<",item.replace("\n",","),">\n") for item in pat.findall(s)])
  15.  
  16. def test5(s):
  17.     fList = s.split('>\n<')
  18.     return ',>\n'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList])
  19.  
  20. if __name__ == '__main__':
  21.     from timeit import Timer
  22.     s = '<\nline 1\nline 2\nline 3\nline < 4\nline > 5\n>\n<\nline 1\nline < 2\nline >3\n>\n<\nline 1\nline < 2\nline >3\n>\n<\nline 1\nline < 2\nline >3\n>\n<\nline 1\nline 2\nline 3\nline < 4\nline > 5\n>\n<\nline 1\nline 2\nline 3\nline < 4\nline > 5\n>'
  23.     t = Timer("test1(s)", "from __main__ import test1, s")
  24.     print t.timeit(100000)
  25.     t = Timer("test4(s)", "from __main__ import test4, s")
  26.     print t.timeit(100000)
  27.     t = Timer("test5(s)", "from __main__ import test5, s")
  28.     print t.timeit(100000)
Output (executed three times):
Expand|Select|Wrap|Line Numbers
  1. >>> 5.90535333401
  2. 4.68323404232
  3. 4.66921242784
  4. >>> 5.84924620308
  5. 4.71450198279
  6. 4.44999015238
  7. >>> 5.91304453499
  8. 4.73270178193
  9. 4.4917451799
  10. >>> 
Not much difference! If you leave out the import statement, the 're' solution (test4()) ends up slightly faster than test5().
Apr 26 '07 #8
bvdet
2,851 Recognized Expert Moderator Specialist
Correction -
Expand|Select|Wrap|Line Numbers
  1. fList = open('text_data').read().split('>\n<')
  2. f = open('csv_data', 'w')
  3. f.write(',>\n<'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList]))
  4. f.close()
Expand|Select|Wrap|Line Numbers
  1. def test5(s):
  2.     fList = s.split('>\n<')
  3.     return ',>\n<'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList])
I left the character '<' out in my previous post.
Apr 26 '07 #9
bvdet
2,851 Recognized Expert Moderator Specialist
Here's a winner (judged by timeit):
Expand|Select|Wrap|Line Numbers
  1. def test6(s):
  2.     return ',>\n<,'.join([item.strip().replace('\n', ',') for item in s.split('>\n<')])
  3.  
  4. if __name__ == '__main__':
  5.     from timeit import Timer
  6.     t = Timer("test6(s)", "from __main__ import test6, s")
  7.     print t.timeit(100000)
  8.  
  9. >>> 1.79765081874
Apr 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: kittu_phani | last post by:
Hi I want to transfer the data in text files into Ms Access using a C program. Please suggest me the code with supporting files. Thanks in advance Krishna -- Posted via http://dbforums.com
1
by: davihigh | last post by:
Dear Friends: Wondering that is there neat way to do "subject line" in Python? I am talking about Python 2.4 with Win32 extension installed. The locale can be any of ANSI defined, for example,...
4
by: AHP | last post by:
Hi, I'm using Visual Studio 2005. I am developing a web application that uses the FileUpload control to upload text files to a directory on a webserver. This works fine. However, for me to be...
0
by: klaydze | last post by:
i have created program that convert txt file to .tiff file. but i cant work out in jpg. anyone can give me a line of code that can i use?thx in advance
5
by: gaya3 | last post by:
Hi all, How to convert text file to excel file in java?? pl any one help me out... is ter any api..??
1
by: ashu0720 | last post by:
how to convert text file into image file in VC++
0
by: binh2807 | last post by:
Hi! Psl help me I have a morse application. I want to convert morse text file into wav file or mp3 file for me play it by windows media player. How can i do it. Thanks.
1
by: agarwalsunitadhn | last post by:
Hi I am developing an application in which i need to convert a text file into a resource file and then serach different resources from the resource file. I want to know how to create the resource...
0
by: ravitunk | last post by:
hello....Can anyone tell me how to convert a text file to PDF using VB6...Plz reply soon..I will be thankful...
15
by: pakerly | last post by:
How would i do this, convert a test file to excel? Lets say my text file has fields like this: NUMBER NAME ADDRESS PHONE 11002 Test1 ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.