Question

I have this simple txt file:

[header]
width=8
height=5
tilewidth=175
tileheight=150

[tilesets]
tileset=../GFX/ts1.png,175,150,0,0

[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1

I want to separate the text by the "[header]", "[tilesets]" and "[layers]". Problem is, if I split it in this way:

m = open(self.fullPath, 'r+')
sliced = m.read().split() # Default = \n
print sliced

It shall separate each line, because read() always leave a '\n' at the end of every line:

['[header]', 'width=8', 'height=5', 'tilewidth=175', 'tileheight=150', '[tilesets]', 'tileset=../GFX/ts1.png,175,150,0,0', '[layer]', 'type=Tile', 'Layer', '1', 'data=', '1,1,1,1,1,1,1,1,', '1,0,0,0,0,0,0,1,', '1,0,0,0,0,1,1,1,', '1,0,0,0,6,0,0,1,', '1,1,1,1,4,1,1,1']

But it's possible to split perfectly if, instead a new-line-character, there was a "#" sign or whatever separating each section.

Then, I thought: "There are empty lines there, and they are new-line-characters, so I just need to test if the line equals to the new-line-character and replace it with '#'":

for line in m.readlines():
    if line == '\n':
        m.write('#')
for line in m.readlines():
    print line

Perfect.. Except that.. Instead of achieving this:

[header]
width=8
height=5
tilewidth=175
tileheight=150
#
[tilesets]
tileset=../GFX/ts1.png,175,150,0,0
#
[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1

I get this:

[header]
width=8
height=5
tilewidth=175
tileheight=150

[tilesets]
tileset=../GFX/ts1.png,175,150,0,0

[layer]
type=Tile Layer 1
data=
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,6,0,0,1,
1,1,1,1,4,1,1,1##õÙÓ              Z                  d                Z                 d                 d                 l                 Z                 d                 d                 l                 Z                 d          "      Z                 d          $      „                  Z                 d          -      f                  d                 „                  ƒ                   Y                  Z                 e          H      d                 ƒ                 Z                 e          I      j                 ƒ                                  Æ Çîà  õÙÓ        ;      |                  j                 d                 ƒ                 }                 i          <      g                  d                  6                  g                  d                  6                  g                  d                  6                  }                 d          =      d                 d                 g                 }                 x   0    ·ð?      |                 j                 ƒ                   D                                                                                                                   u tîà  õÙÓI   À¶ð                                                                                                                                                                                                                                                                                                                ) (–à    W                                                                                                                                                                                                                                                                                                                           # "íà  õ@ÎÔ   €·ðB      |                 j                 ƒ                   D                                                                                                                                                                                                                                           ú  ú–à  õ(Tò   `·ð       }                 |          C       G                   H                  q   |   @·ð                                                                                                                                                                                                            Ñ  Ñ–à  õ@ÎÔ                                                                                                                                                                                                                                                                                                                        ¨  ¨–à  õ@ÎÔ
       E       G                   H                  |          F      j                 ƒ                                                                                                                                                                                                                        –à  õ@ÎÔ                S                                                                                                                                                                                                                                                                                                       V  V–à  õ@ÎÔž   ÿÿÿÿ    t  |  j d ƒ } i g  d 6g  d 6g  d 6} d d d g } x0 | j ƒ  D]" } | d k rk | j d  ƒ n  qI Wx | j ƒ  D] } | GHq| Wd
 GH| j ƒ  d  S `:ð>  >§à  õ@ÎÔÀ:ðà¢îðà:ð ;ð`ßî ;ð@;ð0ð`;ð £îXð@ ï€;ð€ð ;ð`£îÀ;ðà;ð ð2  2›à  õ@ÎÔ`<ð€<ðà¤î <ð ?îÀ<ðà<ð =ð =ð@=ðÀ?î ïÐð`=ð¸ï€=ð =ðøðÀ=ðà=ð >ð >ð@>ð`>ð ð€>ð >ðÀ>ðà>ð ?ð@OÑ ?ð@?ð`?ð€?ð ?ðHðpðÀ?ð˜ðÀðà?ð @ðÀ£î@@ð`@ð€@ð @ð PðHPðÀ@ðà@ð

It makes no sense :).

Was it helpful?

Solution

Simultaneously reading from and writing to a file tends to have unpredictable effects on what kind of output you get.

If your categories are always separated by two newlines, then just split on that, instead of doing any fancy find/replace operations.

m = open("input.txt", "r+")
sliced = m.read().split("\n\n")
print "data has been split into {} categories.".format(len(sliced))
#print the starting line of each category
for category in sliced:
    print category.split("\n")[0]

Result:

data has been split into 3 categories.
[header]
[tilesets]
[layer]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top