简体   繁体   中英

Using csv.reader on clipboard data

I use csv.reader successfully on a .csv file but if I copy the contents of the same file to the clipboard using Notepad++ and feed that to the csv.reader, I get a different result. Each character on the string gets written to a separate row.

eg

import csv
import win32clipboard

win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()

board = csv.reader(data, dialect='excel')
for row in board:
    print(','.join(row))

Any ideas? Thank you already.

Your problem is that you hand csv.reader a string, but it expects some iterable that yields a line for every call of next . A string is iterable, but yields one character each.

The solution is to hand csv.reader a list of lines:

board = csv.reader(data.splitlines(), dialect="excel")

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