0

I'm having trouble writing .txt with python. So i first read one .txt file, then split it, and rearrange then write it out to another .txt file.

Here's my code

# Read objects 
objectit = open('objects.txt', 'r') 
Lines = objectit.readlines() 


# SpawnObjects writing
count = 0
objectit = open('SpawnObject.txt', 'w')
for line in Lines:
    test = line.split('|', 3)
    objline = ("SpawnObject( ", "\"",test[0],"\"", ", " , "\"",test[1],"\"", ", " , "\"",test[2],"\"" , ");\n")
    print("SpawnObject( ", "\"",test[0],"\"", ", " , "\"",test[1],"\"", ", " , "\"",test[2],"\"" , ");\n")
    objectit.writelines(objline)
objectit.close() 

Input data looks like this:

bldr_Game_wall_5m|13751.722656 46.671505 2962.292480|-155.094345 0.000000 -2.000000
Barrier_4m|13783.922852 41.197544 2915.867920|134.620407 0.000000 2.000000
randomObject|13793.672852 40.633644 2912.021484|136.797043 0.000000 3.000002

And this is what i get when i run this through script.

SpawnObject( "bldr_Game_wall_5m", "13751.722656 46.671505 2962.292480", "-155.094345 0.000000 -2.000000
");
SpawnObject( "Barrier_4m", "13783.922852 41.197544 2915.867920", "134.620407 0.000000 2.000000
");
SpawnObject( "randomObject", "13793.672852 40.633644 2912.021484", "136.797043 0.000000 3.000002
");

I don't know why ");" is going to another row. But i've tried multiple things to fix this. Whole point was to automate this step, and currently i still have to do manual labor

To be clear, below is the example of data i'd like to get.

SpawnObject( "randomObject", "13793.672852 40.633644 2912.021484", "136.797043 0.000000 3.000002");
SpawnObject( "Barrier_4m", "13783.922852 41.197544 2915.867920", "134.620407 0.000000 2.000000");
SpawnObject( "randomObject", "13793.672852 40.633644 2912.021484", "136.797043 0.000000 3.000002");
Jack Ham
  • 3
  • 3
  • Does this answer your question? [How to read a text file into a string variable and strip newlines?](https://stackoverflow.com/questions/8369219/how-to-read-a-text-file-into-a-string-variable-and-strip-newlines) – Brian McCutchon Jul 15 '20 at 15:32

2 Answers2

1

line ends with a newline, so test[2] has a newline at the end of it.

Use rstrip() to remove the newline:

for line in Lines:
    test = line.rstrip().split('|')
    ...
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Your "objects.txt" file has new lines in it and you are copying them over when you print "test[2]".

You can't see them, but your file looks like this:

bldr_Game_wall_5m|13751.722656 46.671505 2962.292480|-155.094345 0.000000 -2.000000\n
Barrier_4m|13783.922852 41.197544 2915.867920|134.620407 0.000000 2.000000\n
randomObject|13793.672852 40.633644 2912.021484|136.797043 0.000000 3.000002\n

You need to remove the newlines. A solution to this is to "strip trailing white space" with rstrip().

# Read objects 
objectit = open('objects.txt', 'r') 
Lines = objectit.readlines() 


# SpawnObjects writing
count = 0
objectit = open('SpawnObject.txt', 'w')
for line in Lines:
    test = line.rstrip().split('|', 3) # <---- CHANGE HERE
    objline = ("SpawnObject( ", "\"",test[0],"\"", ", " , "\"",test[1],"\"", ", " , "\"",test[2],"\"" , ");\n")
    print("SpawnObject( ", "\"",test[0],"\"", ", " , "\"",test[1],"\"", ", " , "\"",test[2],"\"" , ");\n")
    objectit.writelines(objline)
objectit.close() 
Brennen Sprimont
  • 1,564
  • 15
  • 28
  • Thanks! I actually didn't know that, but you learn from your mistakes. Nonetheless, works flawlessly now! – Jack Ham Jul 14 '20 at 21:43