0

I have data like this form:

X=[]; 
X=[X,0.114]; 
X=[X,0.749]; 
X=[X,0.358]; 
    .
    .
    .

I want to extract the values in the lists in a single list, as:

X = [0.114, 0.749, 0.358,...]

I used this code but it doesn't work:

import pandas as pd
data = pd.read_csv('Xtest.txt')
count = data['X']

Could you please help me pull the values? And thank you.

1
  • How is X=[X,0.114]; X=[X,0.749]; X=[X,0.358]; CSV format? Commented May 3, 2020 at 14:59

2 Answers 2

2

How about plain python? Something like

X = []
with open('Xtest.txt', 'r') as file:
    lines = file.readlines()
    for line in lines:
        try:
            X.append(float(line.split(',')[1].split(']')[0]))
        except Exception:
            continue
print(X)
Sign up to request clarification or add additional context in comments.

2 Comments

It returns a blank list [ ]. Actually I edited my question, I think that my data is not sorted as CSV file, it is just a .txt file where the data are in that form.
I made an edit to my answer kindly try Again. I replaced X.append(int(line.split(',')[1].split(']')[0])) with X.append(float(line.split(',')[1].split(']')[0]))
0

If 0.114, 0.749, 0.358,..are the values in column1 of the CSV file, the answer is as follows,

import pandas as pd
data = pd.read_csv('Xtest.txt')
y = data[0].to_list()
print(y)

1 Comment

It doesn't work. Sorry, I edited my question, I think that my data is not sorted as CSV file, it is just a .txt file where the data are in that form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.