1

I have written a code in Python 3 that extracts values from a table(.csv) using pandas and appends them to a list. My code is:

import os
import numpy as np
import csv
import pandas as pd
from generate_xml import write_xml

# global constants
img = None
tl_list = []
br_list = []
object_list = []
tl_x = []
tl_y = []
br_x =[]
br_y =[]


# constants
obj = 'red_hat'

df = pd.read_csv('ring_1_05_sam.csv')
tl_x = df.iloc[5:10, 0:1] - 30
tl_y = df.iloc[5:10, 1:2] - 30
br_x = df.iloc[5:10, 0:1] + 30
br_y = df.iloc[5:10, 1:2] + 30
tl_x = (tl_x.to_string(index=False, header=False))
tl_y = (tl_y.to_string(index=False, header=False))
br_x = (br_x.to_string(index=False, header=False))
br_y = (br_y.to_string(index=False, header=False))
tl_list = (tl_x, tl_y)
br_list = (br_x, br_y)
object_list.append(obj)

print(tl_list[0])

This gives me an output of

1507.50
1507.44
1507.09
1507.00

So as you can see, my index[0] at tl_list[] has these 4 elements. My question is how do i access them individually? To be more specific, I want to pass each value to a function, one at a time

import os
import numpy as np
import csv
import pandas as pd
from generate_xml import write_xml

# global constants
img = None
tl_list = []
br_list = []
object_list = []
tl_x = []
tl_y = []
br_x =[]
br_y =[]


# constants
obj = 'red_hat'

df = pd.read_csv('ring_1_05_sam.csv')
tl_x = df.iloc[5:10, 0:1] - 30
tl_y = df.iloc[5:10, 1:2] - 30
br_x = df.iloc[5:10, 0:1] + 30
br_y = df.iloc[5:10, 1:2] + 30
tl_x = (tl_x.to_string(index=False, header=False))
tl_y = (tl_y.to_string(index=False, header=False))
br_x = (br_x.to_string(index=False, header=False))
br_y = (br_y.to_string(index=False, header=False))
tl_list = (tl_x, tl_y)
br_list = (br_x, br_y)
object_list.append(obj)

write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None

So that my write_xml function takes the first value at tl_list and br_list then the next value sequentially. Right now it tales all values at once. Any ideas how i can do it?

1 Answer 1

1

You can split the string and iterate through the resulting list:

for tl in tl_list[0].split():
    write_xml(image_folder, img, object_list, tl, br_list, savedir)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I will try it out as soon as i have time and let you know if it works
i get this error when i try to run the code for tl in tl_list[0].split(): AttributeError: 'tuple' object has no attribute 'split'
I think you must missed the [0] part.
This is the input i gave for tl in tl_list[0].split(): write_xml(image_folder, img, object_list, tl, br_list, savedir)
Well in that case tl_list[0] is not a string but a tuple. Try tl_list[0][0].split() then.

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.