Hello All,
I have a text file in the following format -
<
-
line 1
-
line 2
-
line 3
-
line < 4
-
line > 5
-
>
-
<
-
line 1
-
line < 2
-
line >3
-
>
-
and so on
I need to convert it to csv so it looks like this: -
<,line 1,line 2,line 3,line < 4,line > 5,>
-
<,line 1,line < 2,line >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
9 12611 bartonc 6,596
Recognized Expert Expert
Hello All,
I have a text file in the following format -
<
-
line 1
-
line 2
-
line 3
-
line < 4
-
line > 5
-
>
-
<
-
line 1
-
line < 2
-
line >3
-
>
-
and so on
I need to convert it to csv so it looks like this: -
<,line 1,line 2,line 3,line < 4,line > 5,>
-
<,line 1,line < 2,line >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): - theData = open(r"the\file\path\filename.txt")
-
for line in theData:
-
marker = line[0]
-
if marker == "<":
-
while marker != ">":
-
nextLine = theData.next()
-
print nextLine
-
marker = nextLine[0]
Something like that, anyway. It shoud be enough to get you started.
Something like that, anyway. It shoud be enough to get you started.
thanks - I'll give it a whirl
bvdet 2,851
Recognized Expert Moderator Specialist
Hello All,
I have a text file in the following format -
<
-
line 1
-
line 2
-
line 3
-
line < 4
-
line > 5
-
>
-
<
-
line 1
-
line < 2
-
line >3
-
>
-
and so on
I need to convert it to csv so it looks like this: -
<,line 1,line 2,line 3,line < 4,line > 5,>
-
<,line 1,line < 2,line >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: - fList = [s.strip() for s in open('text_to_csv.txt').readlines()]
-
-
outStr = ''
-
for i in fList:
-
if i == '>':
-
outStr += (i+'\n')
-
else:
-
outStr += (i+',')
-
-
f = open('text_to_csv.csv', 'w')
-
f.write(outStr.strip())
-
f.close()
This uses string concatenation. It is not efficient, but it works
works perfectly for me - thanks!
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.
-
data=open("file").read()
-
pat = re.compile("^<$(.*?)^>$",re.M|re.DOTALL)
-
for item in pat.findall(data):
-
item=item.replace("\n",",")
-
print "<",item,">"
-
output: -
# ./test.py
-
< ,line 1,line 2,line 3,line < 4,line > 5, >
-
< ,line 1,line < 2,line >3, >
-
bvdet 2,851
Recognized Expert Moderator Specialist - fList = open('text_data').read().split('>\n<')
-
f = open('csv_data', 'w')
-
f.write(',>\n'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList]))
-
f.close()
So far we have three methods that do basically the same thing. Let's see what timeit shows us: - def test1(s):
-
fList = [i.strip() for i in s.split('\n')]
-
outStr = ''
-
for i in fList:
-
if i == '>':
-
outStr += (i+'\n')
-
else:
-
outStr += (i+',')
-
return outStr
-
-
def test4(s):
-
import re
-
pat = re.compile("^<$(.*?)^>$",re.M|re.DOTALL)
-
return ''.join(['%s%s%s' % ("<",item.replace("\n",","),">\n") for item in pat.findall(s)])
-
-
def test5(s):
-
fList = s.split('>\n<')
-
return ',>\n'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList])
-
-
if __name__ == '__main__':
-
from timeit import Timer
-
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>'
-
t = Timer("test1(s)", "from __main__ import test1, s")
-
print t.timeit(100000)
-
t = Timer("test4(s)", "from __main__ import test4, s")
-
print t.timeit(100000)
-
t = Timer("test5(s)", "from __main__ import test5, s")
-
print t.timeit(100000)
Output (executed three times): - >>> 5.90535333401
-
4.68323404232
-
4.66921242784
-
>>> 5.84924620308
-
4.71450198279
-
4.44999015238
-
>>> 5.91304453499
-
4.73270178193
-
4.4917451799
-
>>>
Not much difference! If you leave out the import statement, the 're' solution (test4()) ends up slightly faster than test5().
bvdet 2,851
Recognized Expert Moderator Specialist
Correction - - fList = open('text_data').read().split('>\n<')
-
f = open('csv_data', 'w')
-
f.write(',>\n<'.join([','.join([i for i in item.split('\n') if i != '']) for item in fList]))
-
f.close()
- def test5(s):
-
fList = s.split('>\n<')
-
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.
bvdet 2,851
Recognized Expert Moderator Specialist
Here's a winner (judged by timeit): - def test6(s):
-
return ',>\n<,'.join([item.strip().replace('\n', ',') for item in s.split('>\n<')])
-
-
if __name__ == '__main__':
-
from timeit import Timer
-
t = Timer("test6(s)", "from __main__ import test6, s")
-
print t.timeit(100000)
-
-
>>> 1.79765081874
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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,...
|
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...
|
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
|
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..??
|
by: ashu0720 |
last post by:
how to convert text file into image file in VC++
|
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.
|
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...
|
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...
|
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 ...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |