我的文件是这样的:
raw_file
-->
'Date\tValue\tSeries\tLabel\n07/01/2007\t687392\t31537611\tThis home\n08/01/2007\t750624\t31537611\tThis home\n09/01/2007\t769358\t31537611\tThis home\n10/01/2007\t802014\t31537611\tThis home\n11/01/2007\t815973\t31537611\tThis home\n12/01/2007\t806853\t31537611\tThis home\n01/01/2008\t836318\t31537611\tThis home\n02/01/2008\t856792\t31537611\tThis home\n03/01/2008\t854411\t31537611\tThis home\n04/01/2008\t826354\t31537611\tThis home\n05/01/2008\t789017\t31537611\tThis home\n06/01/2008\t754162\t31537611\tThis home\n07/01/2008\t749522\t31537611\tThis home\n08/01/2008\t757577\t31537611\tThis home\n'
type(raw_file)
-->;<type 'str'>
出于某种原因,I can't use pd.read_csv(raw_file)
或者我会得到错误:
File "pandas\_libs\parsers.pyx", line 710, in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8873)
IOError: File Date Value Series Label
07/01/2007 687392 31537611 This home
08/01/2007 750624 31537611 This home
does not exist
我能想到的最好的办法是:
for row in raw_file.split('\n'):
print(row.split('\t'))
这很慢。有更好的办法吗?你知道吗
当你给熊猫一个
string
作为filepath_or_buffer
参数时,它认为它是一个文件名或URL。你知道吗从docs:
解决方案:使用
io.StringIO()
构造函数:为什么不使用
csv
模块并将分隔符设置为\t
?你知道吗https://docs.python.org/3.4/library/csv.html
与csv.reader文件(您的\u文件,分隔符='\t')作为f: #做些事情
相关问题 更多 >
编程相关推荐