Python Forum
csv.reader(): Limit the number of columns read in Windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv.reader(): Limit the number of columns read in Windows
#10
Using a copy of the file you pasted in your last message stored on my machine in "utf-8" encoding, this seems to work:

import csv

file1 = 'C:/Users/MyUserId/envtest/testutf.csv'
with open(file1, encoding="utf-8") as fp:
    crdr = csv.reader(fp, delimiter = ',')
    for data1 in crdr:
        print(data1)
Output:
['', '', '', '', 'max Attendance', '', '', 'Max Score', '425', '', '', '', '50', '25', '', '50', '25', '', '50', '25', '', '50', '25', '', '50', '25', '', '0', '25', '', '0', '25', ''] ['', '', '', '23名', '8', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] ['Photo', 'phone', '学号', '姓名', '出勤', '出勤率', '期末分数', '得分率', '得分', 'HW', 'CW', 'Week 16', 'HW', 'CW', 'Week 15', 'HW', 'CW', 'Week 14', 'HW', 'CW', 'Week 13', 'HW', 'CW', 'Week 12', 'HW', 'CW', 'Week 11', 'HW', 'CW', 'Week 10', 'HW', 'CW', 'Week 9'] ['', '', '1921030146', '聂林', '0', '0', '0', '0', '0', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'] ['', '', '1921090147', '郜阳杰', '0', '0', '0', '0', '0', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'] ['', '', '1922190144', '汪毓韬', '0', '0', '0', '0', '0', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'] ['', '', '1923010312', '张长乐', '7', '100', '100', '100', '425', '', '', '', '50', '25', '1', '50', '25', '1', '50', '25', '1', '50', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1925010121', '陈尚', '4', '57.1', '47.6', '41.2', '175', '', '', '', '0', '25', '1', '50', '25', '1', '0', '0', '0', '0', '25', '0', '0', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926070127', '蒲瑞杰', '6', '85.7', '83.7', '82.4', '350', '', '', '', '50', '25', '1', '50', '25', '1', '50', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926070150', '张旭', '1', '14.3', '9.3', '5.9', '25', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '25', '1', '0', '0', '0', '0', '0', '0'] ['', '', '1926080140', '陈子华', '0', '0', '0', '0', '0', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'] ['', '', '1926080205', '王天亮', '5', '71.4', '56.8', '47.1', '200', '', '', '', '0', '25', '0', '0', '25', '1', '0', '25', '1', '0', '25', '1', '50', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926080209', '闫怀壮', '3', '42.9', '27.7', '17.6', '75', '', '', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926080216', '朱增援', '6', '85.7', '84.4', '83.5', '355', '', '', '', '50', '25', '0', '40', '25', '1', '0', '25', '1', '40', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1926080230', '杨澜', '6', '85.7', '62.5', '47.1', '200', '', '', '', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '50', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926080234', '张建军', '6', '85.7', '62.5', '47.1', '200', '', '', '', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '50', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1926080243', '游杰', '7', '100', '100', '100', '425', '', '', '', '50', '25', '1', '50', '25', '1', '50', '25', '1', '50', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1926080254', '王润迪', '6', '85.7', '62.5', '47.1', '200', '', '', '', '0', '25', '1', '0', '25', '1', '0', '0', '0', '0', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1927040125', '黄楚兰', '7', '100', '91.5', '85.9', '365', '', '', '', '50', '25', '1', '50', '25', '1', '0', '25', '1', '40', '25', '1', '50', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1928040103', '曹予曦', '5', '71.4', '46.2', '29.4', '125', '', '', '', '0', '0', '0', '0', '25', '1', '0', '0', '0', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1928040213', '严昕', '5', '71.4', '53.3', '41.2', '175', '', '', '', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '50', '25', '1', '0', '0', '0', '0', '0', '0'] ['', '', '1929020321', '伍竹', '5', '71.4', '46.2', '29.4', '125', '', '', '', '0', '25', '1', '0', '0', '0', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '0', '0'] ['', '', '1929020325', '张清源', '6', '85.7', '55.5', '35.3', '150', '', '', '', '0', '25', '1', '0', '0', '0', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1929020329', '章明慧', '7', '100', '57.6', '29.4', '125', '', '', '', '0', '25', '1', '0', '25', '1', '0', '0', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '0', '1'] ['', '', '1929030133', '丁桃园', '7', '100', '70.4', '50.6', '215', '', '', '', '0', '25', '1', '40', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1', '0', '25', '1'] ['', '', '1929050140', '石楷', '7', '100', '90.1', '83.5', '355', '', '', '', '50', '25', '1', '0', '25', '1', '50', '25', '1', '50', '25', '1', '30', '25', '1', '0', '25', '1', '0', '25', '1']
I hope that helps you.

Peter
snippsat likes this post
Reply


Messages In This Thread
RE: csv.reader(): Limit the number of columns read in Windows - by pjfarley3 - Jan-23-2021, 01:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read complete windows registry? fredep57 3 959 Mar-15-2023, 08:14 PM
Last Post: buran
  Read directory listing of files and parse out the highest number? cubangt 5 2,380 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  xml simple reader kucingkembar 2 1,065 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,503 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,489 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Remove Specific Columns when the number of columns is greater than a specific value CuriousOne 0 1,326 Sep-09-2021, 09:17 PM
Last Post: CuriousOne
  NFC reader code help johnroberts2k 1 2,600 Jul-02-2021, 08:43 PM
Last Post: deanhystad
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,580 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Read strings and numbers in columns from a file suvadip 4 2,918 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Closing Files - CSV Reader/Writer lummers 2 2,624 May-28-2020, 06:36 AM
Last Post: Knight18

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020