Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign points in a text file to be x and y in a list

I have a text file that has data of x , y like this:

-10.61118889    2.28427887
-13.31124592    8.48367596
-13.66134357    8.83389187
-12.8115654 1.28432655
-11.71096611    6.03327417
-12.76088619    5.83374786
-14.01123905    8.38309383
-14.71052933    8.78343105
-12.61050415    9.18339539

I want to create a list that reads each line and assigns the first no. as x and the second as y, something like this:

[(-10.61118889,2.28427887),(-13.31124592,8.48367596) ....]

I have tried this but it didn’t work as it has constructed a table with header x and y:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
df = pd.read_table('convex_hull_points.txt', sep=' ')
df.to_csv('convex_hull_points.csv', index=False)
data = pd.read_csv('convex_hull_points.csv', sep=' ',  header=None, names=['X','Y'])

>Solution :

Not sure why the OP thinks pandas is necessary for this. To produce a list of tuples as shown in the question:

list_ = []

with open('convex_hull_points.txt') as chp:
  for line in chp:
    x, y = line.split()
    list_.append((float(x), float(y)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading