如何将这样的数据导入R或Python?
我希望导入此处找到的数据,使其成为691x7表。我如何告诉它识别不同的列如何将这样的数据导入R或Python?,python,r,import,Python,R,Import,我希望导入此处找到的数据,使其成为691x7表。我如何告诉它识别不同的列 提前感谢您现在很可能已经解决了您的问题。如果没有,这里有一个建议:你可以写一些正则表达式来阅读这些行,但在我看来这太麻烦了。在我看来,定义列的特性是它们的大小。所以你得数一数,剩下的就简单了。进行第一次计数,然后通过这样做(这是您稍后需要的代码)将结果可视化: 一旦尺寸合适: with open('shortsell.txt', 'r') as file: [next(file) for _ in range(5)
提前感谢您现在很可能已经解决了您的问题。如果没有,这里有一个建议:你可以写一些正则表达式来阅读这些行,但在我看来这太麻烦了。在我看来,定义列的特性是它们的大小。所以你得数一数,剩下的就简单了。进行第一次计数,然后通过这样做(这是您稍后需要的代码)将结果可视化: 一旦尺寸合适:
with open('shortsell.txt', 'r') as file:
[next(file) for _ in range(5)] # Skip the first 5 rows
# Read the columns parts
columns = [[line[:8].strip(), line[8:42].strip(), line[42:56].strip(),
line[56:71].strip(), line[71:90].strip(), line[90:].strip()]
for line in (next(file), next(file), next(file))]
# Join the parts
columns = [' '.join(columns[i][j] for i in range(3)).strip() for j in
range(6)]
# Read the data and cast to fitting type
data = [[line[:8].strip(),
line[8:42].strip(),
line[42:56].strip(),
int(line[56:71].strip().replace(',', '')),
int(line[71:90].strip().replace(',', '')),
float(line[90:].strip().replace(',', ''))]
for line in file]
结果:
['ASX Code',
'Company Name',
'Product/ Class',
'Reported Gross Short Sales (a) ASX + CHI-X',
'Issued Capital (b)',
'% of issued capital reported as short sold (a)/(b)']
['ASX Code',
'Company Name',
'Product/ Class',
'Reported Gross Short Sales (a) ASX + CHI-X',
'Issued Capital (b)',
'% of issued capital reported as short sold (a)/(b)']
[['360', 'LIFE360 INC.', 'CDI FORUS', 8999, 148866201, 0.0],
['3DP', 'POINTERRA LIMITED', 'FPO', 15213, 670733112, 0.0],
['4DS', '4DS MEMORY LIMITED', 'FPO', 15000, 1310693486, 0.0],
...
['ZEL', 'Z ENERGY LIMITED.', 'FPO NZX', 23255, 520476853, 0.0],
['ZLD', 'ZELIRA THERAPEUTICS LIMITED', 'FPO', 101860, 1185322966, 0.0],
['ZNO', 'ZOONO GROUP LIMITED', 'FPO', 67213, 163612707, 0.04]]