-1

Lets say I have a .txt file that the first few lines of data looks like this:

v 0.188267 3.068606 0.003011 v -0.188267 3.068606 0.003011 v 0.168431 3.068606 0.013613 v -0.168431 3.068606 0.013613 v 0.146908 3.068606 0.020142 v -0.146908 3.068606 0.020142 v 0.124524 3.068606 0.022347

Ok, my goal is to be able to import the file into pycharm. And then I want to write an algorithm that turns the data into this format:

(0.188267, 3.068606, 0.003011), (-0.188267, 3.068606, 0.003011), (0.168431, 3.068606, 0.013613), (-0.168431, 3.068606, 0.013613), (0.146908, 3.068606, 0.020142), (-0.146908, 3.068606, 0.020142), (0.124524, 3.068606, 0.022347),

Then from there I want the data to be exported into another .txt file. In actuality the original .txt file is over 40,000 lines and doing this manually is tedious and time consuming.

Manly Winter
  • 21
  • 1
  • 9
  • What have you tried? Are you able to open the file? Once you have the data, have you tried using `.split()` on the imported lines? – pythomatic Nov 27 '18 at 19:36
  • https://stackoverflow.com/questions/14676265/how-to-read-a-text-file-into-a-list-or-an-array-with-python this is kind of the same, different format though – prophet-five Nov 27 '18 at 19:41
  • I'm new to python, like 2 days new to it. I don't even know how to include a text file in a directory so that pycharm can read the file. Like, instead of using the complete filepath, I just want to be able to use the text.txt without all the C:/users/... bit. And then from there, I don't know where to begin writing an algorithm that can traverse each line to change the data into the data I want. – Manly Winter Nov 27 '18 at 19:45
  • You should learn to use python from the command line. It's much less of a headache. – Shay Lempert Nov 27 '18 at 20:13
  • I edited my answer and it works now. – Shay Lempert Nov 27 '18 at 20:15

1 Answers1

0
with open("file.txt") as f:
    string = f.read()

formatted_string = string.replace("v ","")
formatted_string = formatted_string.replace("\n"," ")
str_numbers = formatted_string.split(" ")

with open("output.txt","w") as f:
    for i,num in enumerate(str_numbers):
        if i % 3 == 0:
            f.write(f"({float(num)},")
        elif i % 3 == 1:
            f.write(f" {float(num)},")
        else:
            f.write(f" {float(num)}), ")

replace "filename.txt" with the name of your file. And make sure that you don't have spaces at the beginning and end of the file.

Edit: I've tested this on your example.

Shay Lempert
  • 311
  • 2
  • 9
  • This almost works though. On the first line the output in the output text file is: (0.188267, 3.068606, but it stops there. There's no spaces after I Idk. I think I know what the problem is. – Manly Winter Nov 27 '18 at 20:58
  • There's no blank spaces at the end of the file. I don't know why it stops on the first line, the error message reads: f.write(f" {float(num)}), ") ValueError: could not convert string to float: '0.003011\n-0.188267' – Manly Winter Nov 27 '18 at 21:09
  • The problem is probably that I need to replace enters with spaces. Try it now – Shay Lempert Nov 27 '18 at 21:13
  • Could you clarify? How It's on here the lines wrap and isn't side by side like it is on here. That's just how stackoverflow did it when I made the original post. – Manly Winter Nov 27 '18 at 21:23
  • Yes, awesome. It worked but in the output text, how do I get it to stack on top of each other instead of placing them side by side? – Manly Winter Nov 27 '18 at 21:26
  • Like this? : (0.188267, 3.068606, 0.003011),{enter} (-0.188267, 3.068606, 0.003011), – Shay Lempert Nov 27 '18 at 21:27
  • Awesomeness, I figured out the last bit. All I had to do is put \n in the: f.write(f" {float(num)}), \n") Now I can move to the next bit. I appreciate you. – Manly Winter Nov 27 '18 at 21:32
  • Yes, this was the solution. Great! – Shay Lempert Nov 27 '18 at 21:33