Coding noob here with a question. I have a text file that has the following format:
img1.jpg 468,3,489,16,5 510,37,533,51,2 411,3,433,17,5....
img2.jpg 255,397,267,417,2.... . . .
The data is a series of images with information on co-ordinates where there are 5 variables separated by commas, and then a new set of co-ordinates is separated by a space. There are about 500 files and for each file there are variable numbers of co-ordinate groups. I'm wanting to convert this text file into the following kind of format:
File name | Co-ord 1 | Co-ord 2 | Co-ord 3 | Co-ord 4 | Co-ord 5 |
---|---|---|---|---|---|
img1.jpg | 468 | 3 | 489 | 16 | 5 |
img1.jpg | 510 | 37 | 533 | 51 | 2 |
img1.jpg | 411 | 3 | 433 | 17 | 5 |
img2.jpg | 255 | 397 | 267 | 417 | 2 |
How can I do this in python?
Since each image name and group of co-ordinates are separated by spaces, we can use split() to split it info array and it's basically what your expected output.
Here i wrote an example that split your input into a list of lists, each inner list represent one line in your example output with the first element describe which image the co-ordinates belong to:
test = "img1.jpg 468,3,489,16,5 510,37,533,51,2 411,3,433,17,5"
str_list = test.split()
res = list()
recent_img = ''
for item in str_list:
if item.endswith("jpg"):
# find a new image name
recent_img = item
continue
co_ordinates_list = item.split(",")
if len(co_ordinates_list) == 5:
co_ordinates_list.insert(0, recent_img)
res.append(co_ordinates_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.