python 逗号分隔_python中如何将数据行拆分成逗号分隔的列

1.numpy解决方案(因为numpy标记)

对numpy数组使用^{}:import numpy as np

arr = np.genfromtxt('file.txt',dtype='str',delimiter=',')

print (arr)

[['red' 'red' 'blue']

['blue' 'red' 'blue']

['blue' 'blue' 'red']]

print (arr[0])

['red' 'red' 'blue']

print (arr[0][2])

blue

2.pandas解决方案:

对DataFrame和选择值^{}使用^{}:import pandas as pd

df = pd.read_csv('file.txt', header=None)

print (df)

0 1 2

0 red red blue

1 blue red blue

2 blue blue red

#select first row to Series

print (df.loc[0])

0 red

1 red

2 blue

Name: 0, dtype: object

#select value by index and column

print (df.loc[0, 2])

blue

3.纯python解决方案:

如果需要嵌套列表,请使用nested list comprehension:data = [[item for item in line.rstrip('\r\n').split(',')]

for line in open('file.txt')]

print (data)

[['red', 'red', 'blue'], ['blue', 'red', 'blue'], ['blue', 'blue', 'red']]

或者使用模块csv:import csv

reader = csv.reader(open("file.txt"), delimiter=',')

data = [word for word in [row for row in reader]]

print (data)

[['red', 'red', 'blue'], ['blue', 'red', 'blue'], ['blue', 'blue', 'red']]print (data[0])

['red', 'red', 'blue']

print (data[0][2])

blue

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
IPsec ....................................................................................................................................................... 1 IPsec简介................................................................................................................................................... 1 IPsec的协议实现........................................................................................................................................ 1 IPsec基本概念............................................................................................................................................ 2 加密卡 ........................................................................................................................................................ 4 IPsec虚拟隧道接口..................................................................................................................................... 4 使用IPsec保护IPv6路由协议..................................................................................................................... 6 IKE .......................................................................................................................................................... 6 IKE简介...................................................................................................................................................... 6 IKE的安全机制........................................................................................................................................... 6 IKE的交换过程........................................................................................................................................... 7 IKE在IPsec的作用.................................................................................................................................. 8 IPsec与IKE的关系...................................................................................................................................... 8
©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值