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:
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)))