简体   繁体   中英

How to create a list of tuples from two specific columns of a csv file?

My csv file looks something like this:

{http://www.omg.org/XMI}id,begin,end,Character
18074,15,19,Tony
18120,39,46,Tony
18172,129,134,Clint
18217,175,180,Bucky
18202,245,249,Tony
18307,352,357,Bucky
18376,1297,1302,Steve
18421,1499,1504,Bruce
18489,1546,1553,Natasha
18527,1709,1712,Bucky

I would like to be able to create a list of tuples from the columns begin and end , starting from the second row, ignoring the title-row, of course.

So far I can create a list of tuples but for all rows and columns:

import csv
import tkinter
from tkinter import filedialog
root_tk = tkinter.Tk()
root_tk.wm_withdraw()

filename = filedialog.askopenfilename()

with open(filename, 'r') as f:
    data=[tuple(line) for line in csv.reader(f)]

print(data)



root_tk.destroy()
root_tk.mainloop()

Current output:

[('{http://www.omg.org/XMI}id', 'begin', 'end', 'Character'), ('18646', '518', '520', 'Anakin'), ('18699', '982', '985', 'Jedi'), ('18714', '1018', '1020', 'Anakin'), ('18766', '1057', '1059', 'Anakin'),...

Desired output:

[(15, 19), (39,46), (129, 134), (175, 180)...]

How do I limit the output to those two columns, while ignoring the first row and create a list of tuples from them?

Thanks in advance!

EDIT :

I am now able to print the tuples I want, but I can't remove the first row from the output still.

Also, how do I convert the output from a string tuple to integer?

You could use DictReader and create tuples from columns you need

import csv

filename = filedialog.askopenfilename()
with open(filename, 'r') as f:
    data=[(int(line['begin']),int(line['end'])) for line in  csv.DictReader(f)]
    print data

output:

[(15, 19), (39, 46), (129, 134), (175, 180), (245, 249), (352, 357), (1297, 1302), (1499, 1504), (1546, 1553), (1709, 1712)]

Hope this helped :)

You could use pandas

In [615]: df = pd.read_csv('eg.csv')

In [616]: [(begin, end) for _, begin, end, _ in df.values.tolist()]
Out[616]:
[(15, 19),
 (39, 46),
 (129, 134),
 (175, 180),
 (245, 249),
 (352, 357),
 (1297, 1302),
 (1499, 1504),
 (1546, 1553),
 (1709, 1712)]

If you want to use the csv module you can try

In [627]: with open('eg.csv', 'r') as f:
     ...:     csv_data = next(csv.reader(f), None)          # Skip first row
     ...:     data=[(int(line[1]), int(line[2])) for line in csv.reader(f) if line]
     ...:

Or without any imported modules at all

In [639]: with open('eg.csv', 'r') as f:
     ...:     f.readline()               # Skip first row
     ...:     data=[tuple(map(int, line.split(',')[1:3])) for line in f.readlines() if line.strip()]
     ...:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM