Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

Split index on filename gives me wrong output in python

The structure of my folder and file looks like this

images = glob("/content/drive/MyDrive/Colab/input/final_data/)

Inside the final_data I have a couple of files say crash_039.jpg etc. Here I am splitting the part before underscore as my class and the whole part as my image.

I expect my output to have the class names without those square brackets. The code below gives me square brackets and I am not sure what is the issue or is this normal in collab?

images = glob("/content/drive/MyDrive/Colab/input/final_data/*.jpg")
train_image = []
train_class = []
for i in tqdm(range(len(images))):
    # creating the image name
    train_image.append(images[i].split('/')[7])
    # creating the class of image
    train_class.append(images[i].split('/')[7].split('_')[:1])
    
# storing the images and their class in a dataframe
train_data = pd.DataFrame()
train_data['image'] = train_image
train_data['class'] = train_class

# converting the dataframe into csv file 
train_data.to_csv('/content/drive/MyDrive/Colab/input/train_output/train_new.csv',header=True, index=False)

output

    image   class
0   crash_039.jpg   [crash]
1   crash_040.jpg   [crash]

expected output

    image   class
0   crash_039.jpg   crash
1   crash_040.jpg   crash

enter image description here

The attached image shows how the csv is storing it but I do not want those quotations and brackets around the class column.

question from:https://stackoverflow.com/questions/66062882/split-index-on-filename-gives-me-wrong-output-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Slicing like you are doing images[i].split('/')[7].split('_')[:1]) returns you a list which you are printing.

I believe you want to use Index to get classname: images[i].split('/')[7].split('_')[0])

This solves your problem, however I suggest you to use os.path.basename(filename) instead of the messy split('/') you are doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...