【问题标题】:Split data in columns in python在python中的列中拆分数据
【发布时间】:2018-08-01 06:28:54
【问题描述】:

我正在尝试编写一个从外部文件读取一组值的代码,如下所示:

   4.87320984E-09               1       49.882684417026184            0.18691906898647714            0.22222662961584136
   1.88917193E-09               2       49.882684417026184            0.18691906898647714            0.22222662961584136
   1.30942168E-09               3       49.882684417026184            0.18691906898647714            0.22222662961584136
   7.26580629E-10               4       49.882684417026184            0.18691906898647714            0.22222662961584136
   5.39025047E-10               5       49.882684417026184            0.18691906898647714            0.22222662961584136
   2.04686401E-08               1       68.253977742324736            -5.0977927905402787E-002       0.32783928527485512
   3.08666603E-09               3       68.253977742324736            -5.0977927905402787E-002       0.32783928527485512
   2.88387625E-09               4       68.253977742324736            -5.0977927905402787E-002       0.32783928527485512
   1.15430343E-09               5       68.253977742324736            -5.0977927905402787E-002       0.32783928527485512

我想将每一列存储在一个列表中并用它们做一些数学运算。

import sys
inFile = sys.argv[1]
c1 = []
c2 = []
c3 = []
c4 = []
c5 = []
data = [c1,c2,c3,c4,c5]

with open(inFile,'r') as i:
   lines = i.read().split('\n')
for row in lines:
   #split in columns and do some maths
print data

【问题讨论】:

标签: python


【解决方案1】:
with open("inFile",'r') as dataFile:
   for i in dataFile.readlines():
       print i.split()

输出:

['4.87320984E-09', '1', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['1.88917193E-09', '2', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['1.30942168E-09', '3', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['7.26580629E-10', '4', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['5.39025047E-10', '5', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['2.04686401E-08', '1', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['3.08666603E-09', '3', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['2.88387625E-09', '4', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['1.15430343E-09', '5', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']

【讨论】:

    【解决方案2】:
    import pandas as pd
    
    inFile = YOUR_CSV.csv
    df = pd.read_csv(inFile)
    print(df.head(3) #prints first 3 rows to check your data
    

    【讨论】:

      【解决方案3】:

      我肯定会推荐这项任务的熊猫。像这样的东西应该可以工作

      import pandas as pd
      data = pd.read_csv(inFile, delimiter=r'\s+', header=None)
      

      分隔符参数告诉它将一个或多个空格字符视为分隔符。对于您的示例中显示的数据,它应该工作。

      【讨论】:

        【解决方案4】:

        很多人都在推荐 pandas,但由于这个数据只包含数字,你可能只想考虑 numpy.一个很好的教程在这里:https://www.dataquest.io/blog/numpy-tutorial-python/。

        以下是如何导入数据的示例。从那里您可以使用完整的 numpy 库对数组进行数学运算或大量操作。有关更多信息,请参阅我发布的教程。

        import numpy as np
        
        file = 'path_to_your_file.csv'
        
        data_array = np.genfromtxt(file, delimiter=',')
        

        【讨论】:

          【解决方案5】:

          这是您要查找的代码:

          import sys
          inFile = sys.argv[1]
          c1 = []
          c2 = []
          c3 = []
          c4 = []
          c5 = []
          data = [c1,c2,c3,c4,c5]
          
          with open(inFile,'r') as i:
             for row in line:
                 v1, v2, v3, v4, v5 = row.strip().split()
                 c1.append(float(v1))
                 c2.append(float(v2))
                 c3.append(float(v3))
                 c4.append(float(v4))
                 c5.append(float(v5))
          print data
          

          但我建议使用 Pandas read_csv 方法。

          【讨论】:

            猜你喜欢
            • 2020-06-10
            • 1970-01-01
            • 2023-03-30
            • 1970-01-01
            • 2023-01-20
            • 2016-01-08
            • 1970-01-01
            • 1970-01-01
            • 2019-04-18
            相关资源
            最近更新 更多