简体   繁体   English

如何在python中将数据行拆分为以逗号分隔的列

[英]How to split row of data into columns separated by comma in python

I have a text file in the following format that I am trying to convert into rows and columns: 我有一个以下格式的文本文件,试图将其转换为行和列:

red,red,blue
blue,red,blue 
blue,blue,red

Once the conversion is complete, I want to store the above in a rows variable: 转换完成后,我想将以上内容存储在rows变量中:

row[0] # should return 'red red blue'
row[0][2] # should return 'blue'

So far I have gotten as far as: 到目前为止,我已经做到:

file = open('myfile.txt')
for row in file:
    # do something here

But i'm not sure what to do next.. can someone help? 但是我不确定下一步该怎么办..有人可以帮忙吗? Thanks in advance! 提前致谢!

Solution without any external modules : 没有任何外部模块的解决方案:

output = []

with open('file.txt', 'r') as reading:
    file_input = reading.read().split('\n')

for row in file_input:
    output.append(row.split(','))

print(output)

1.numpy solution : (because numpy tag) 1. numpy 解决方案 :(因为numpy标签)

Use numpy.genfromtxt for numpy array: numpy.genfromtxt用于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 solution : 2.pandas解决方案

Use read_csv for DataFrame and for select values loc : read_csv用于DataFrame和选择值loc

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.pure python solutions : 3.纯python解决方案

If want nested lists use nested list comprehension : 如果要嵌套列表,请使用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']]

Or with module csv : 或与模块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

Alternative solution with pandas module which is good for csv files processing: 带有pandas模块的替代解决方案,非常适合csv文件处理:

import pandas as pd

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

print(df[0].tolist())    # ['red', 'red', 'blue']
print(df[0][2])          # blue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将逗号分隔的数据集拆分为 python 中的不同列? - How to split comma separated dataset into different columns in python? 将逗号分隔的数据拆分为python中的列表 - Split data separated by comma into lists in python 如何将逗号分隔的文本拆分为 pandas dataframe 上的列? - How to split comma separated text into columns on pandas dataframe? Python lxml:如何拆分逗号分隔的数据并从 XML 文件中查找特定值? - Python lxml: How to split comma separated data and find specific values from XML-file? 如何在python中拆分单个列表值,用逗号分隔 - how to split a single list value in python which is separated by comma 将数据转置为逗号分隔的行 - transpose of data in a comma separated row Python正则表达式拆分逗号或空格分隔的字符串 - Python regex split comma or space separated string python pandas:将逗号分隔的列拆分为新列 - 每个值一个 - python pandas: split comma-separated column into new columns - one per value 如何使用 python 脚本将一行中存在的数据(以空格分隔)拆分为同一 excel 工作表中的不同列? - How to split data present in a row (separated by space) into different column in the same excel sheet using python script? Python - 将一行拆分为列 - csv数据 - Python - Split a row into columns - csv data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM