0

Example Dataset:

120GB Hard Disk Drive with 3 Years Warranty for Lenovo Essential B570 Laptop Notebook HDD Computer - Certified 3 Years Warranty from Seifelden  3950    8
"TOSHIBA SATELLITE L305-S5919 LAPTOP LCD SCREEN 15.4"" WXGA CCFL SINGLE SUBSTITUTE REPLACEMENT LCD SCREEN ONLY. NOT A LAPTOP"   35099   324
Hobby-Ace Pixhawk PX4 RGB External LED Indicator USB Module for Pixhawk Flight Controller   21822   510
Pelicans mousepad   44629   260
P4648-60029 Hewlett-Packard Tc2100 System Board 42835   68
Ectaco EI900 SD Card English - Italian  249 6
Zippered Pocket Black School Laptop Tablet Dual Straps Deluxe Backpack  4342    172

Here I want to split into three columns

column-1 as Product_id- 120GB Hard Disk Drive with 3 Years Warranty for Lenovo Essential B570 Laptop Notebook HDD Computer - Certified 3 Years Warranty from Seifelden

column-2 as order_id 3950

Column-3 as item_id 8

Likewise I need for all my datasets

1
  • Could you format the content of the dataset as code (Stack overflow automatically removes spaces or tabs in between words) and show us what problems you faced with your own code? Jun 29, 2016 at 14:39

2 Answers 2

1

If you don't mind using a library, pandas can read csvs and tsvs. You'd want

import pandas
df = pandas.read_csv('<your file>', sep='\t', names=['Product_id', 'order_id', 'item_id'])

If you want to use vanilla python, it's a little more complicated, but this stackoverflow question has code snippets that might help.

1

You could use the csv module to read the file:

import csv
from pprint import pprint

columns = 'Product_id order_id item_8'.split()

with open('data.tsv', 'rb') as tsv_file:
    for row in csv.DictReader(tsv_file, fieldnames=columns, delimiter='\t'):
        pprint(row)

Output:

{'Product_id': '120GB Hard Disk Drive with 3 Years Warranty for Lenovo Essential B570 Laptop Notebook HDD Computer - Certified 3 Years Warranty from Seifelden',
 'item_8': '8',
 'order_id': '3950'}
{'Product_id': 'TOSHIBA SATELLITE L305-S5919 LAPTOP LCD SCREEN 15.4" WXGA CCFL SINGLE SUBSTITUTE REPLACEMENT LCD SCREEN ONLY. NOT A LAPTOP',
 'item_8': '324',
 'order_id': '35099'}
{'Product_id': 'Hobby-Ace Pixhawk PX4 RGB External LED Indicator USB Module for Pixhawk Flight Controller',
 'item_8': '510',
 'order_id': '21822'}
{'Product_id': 'Pelicans mousepad', 'item_8': '260', 'order_id': '44629'}
{'Product_id': 'P4648-60029 Hewlett-Packard Tc2100 System Board',
 'item_8': '68',
 'order_id': '42835'}
{'Product_id': 'Ectaco EI900 SD Card English - Italian',
 'item_8': '6',
 'order_id': '249'}
{'Product_id': 'Zippered Pocket Black School Laptop Tablet Dual Straps Deluxe Backpack',
 'item_8': '172',
 'order_id': '4342'}
2
  • Try not to give them teh codez before they show what they tried! Jun 29, 2016 at 14:48
  • @Moon: I'm giving them the benefit of doubt that they did not realize that a tsv is a kind of csv file (or know how to write a proper question).
    – martineau
    Jun 29, 2016 at 15:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.