以逗号分隔的文本存储在如下所示的变量中
data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""
有没有一种快速的方法可以将其读取到熊猫数据框中,而不必存储到文件并使用pd.read_csv()
。我来自R
,它提供了一种不错的方法,如下所示。
text <- "
State,District,County,Num Voters,Total Votes in State,Votes for None,Candidate Name,Party,Votes Scored
CA,San Diego,Delmar,190962,48026634,2511,A1,IND,949
CA,San Diego,Delmar,190962,48026634,2511,A2,RP(K),44815
"
df <- read.table(textConnection(text), sep = ",", header = TRUE)
答案 0 :(得分:7)
带有io.StringIO
对象(文本I / O的内存流):
import pandas as pd
from io import StringIO
data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""
df = pd.read_csv(StringIO(data))
print(df)
输出:
Class Name Long Lat
0 A ABC11 139.629554 35.611441
1 A ABC20 139.630596 35.610456
2 A ABC03 139.630031 35.613278
3 B ABC54 139.778782 35.688479
4 B ABC05 139.781445 35.681688
5 B ABC06 139.778819 35.681865
6 B ABC24 139.779040 35.677817
答案 1 :(得分:0)
from prettytable import PrettyTable
def create_table(data):
data = data.strip().split('\n')
pt = PrettyTable()
pt.field_names = data[0].split(',')
for row in data[1:]:
pt.add_row(row.split(','))
return pt
data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""
table = create_table(data)
print(table)
+-------+-------+-------------+-------------+
| Class | Name | Long | Lat |
+-------+-------+-------------+-------------+
| A | ABC11 | 139.6295542 | 35.61144069 |
| A | ABC20 | 139.630596 | 35.61045559 |
| A | ABC03 | 139.6300307 | 35.61327781 |
| B | ABC54 | 139.7787818 | 35.68847945 |
| B | ABC05 | 139.7814447 | 35.6816882 |
| B | ABC06 | 139.7788191 | 35.681865 |
| B | ABC24 | 139.7790396 | 35.67781697 |
+-------+-------+-------------+-------------+
或
from prettytable import from_csv
from io import StringIO
table = from_csv(StringIO(data))
print(table)