我有以下内容文件.txt(节略):
SICcode Catcode Category SICname MultSIC
0111 A1500 Wheat, corn, soybeans and cash grain Wheat X
0112 A1600 Other commodities (incl rice, peanuts) Rice X
0115 A1500 Wheat, corn, soybeans and cash grain Corn X
0116 A1500 Wheat, corn, soybeans and cash grain Soybeans X
0119 A1500 Wheat, corn, soybeans and cash grain Cash grains, NEC X
0131 A1100 Cotton Cotton X
0132 A1300 Tobacco & Tobacco products Tobacco X
我在把它读成熊猫df时遇到了一些问题。我尝试使用以下规范pd.read_csv
,但它在一列中返回了文件:
然后我试图使用'tab'作为分隔符将其放入gnumeric文件中,但它将文件作为一列读取。有人知道这个吗?在
如果
df = pd.read_csv('file.txt', sep='\t')
返回一个只有一列的数据帧,那么file.txt
显然没有使用制表符作为分隔符。您的数据可能只使用空格作为分隔符。那样的话你可以试试它使用regex模式
^{pr2}$\s{2,}
作为分隔符。此正则表达式匹配2个或多个空白字符。在如果这不起作用,请张贴
print(repr(open(file.txt, 'rb').read(100))
。这将向我们展示file.txt
的前100个字节的明确表示。在如果} 中。在
csv
中的数据用Tabulator
分隔,可以尝试将sep="\t"
添加到^{相关问题 更多 >
编程相关推荐