[英]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.