mch*_*gun 5 python r read.table
我正在尝试将我的一些处理工作从R转移到Python.在R中,我使用read.table()来读取非常混乱的CSV文件,它会以正确的格式自动拆分记录.例如
391788,"HP Deskjet 3050 scanner always seems to break","<p>I'm running a Windows 7 64 blah blah blah........ake this work permanently?</p>
<p>Update: It might have something to do with my computer. It seems to work much better on another computer, windows 7 laptop. Not sure exactly what the deal is, but I'm still looking into it...</p>
","windows-7 printer hp"
Run Code Online (Sandbox Code Playgroud)
正确分为4列.1条记录可以分成很多行,并且到处都有逗号.在RI只做:
read.table(infile, header = FALSE, nrows=chunksize, sep=",", stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)
Python中有什么能够同样做到这一点吗?
谢谢!
您可以使用 csv 模块。
from csv import reader
csv_reader = reader(open("C:/text.txt","r"), quotechar="\"")
for row in csv_reader:
print row
['391788', 'HP Deskjet 3050 scanner always seems to break', "<p>I'm running a Windows 7 64 blah blah blah........ake this work permanently?</p>\n\n<p>Update: It might have something to do with my computer. It seems to work much better on another computer, windows 7 laptop. Not sure exactly what the deal is, but I'm still looking into it...</p>\n", 'windows-7 printer hp']
Run Code Online (Sandbox Code Playgroud)
输出长度 = 4