簡體   English   中英

我如何將這些數據分成兩個單獨的列表,分別為 python 中的 plot?

[英]How would I split these data in to two separate lists to plot in python?

我得到了一個包含光譜數據的大文本文件。

前幾行是這樣的:

397.451 -48.38

397.585 -48.38

397.719 -48.38

397.853 -18.38

397.987 -3.38

398.121 6.62

398.256 -0.38

398.39  -1.38

398.524 7.62

398.658 4.62

398.792 -4.38

398.926 12.62

399.06  5.62

399.194 -6.38

399.328 -6.38

399.463 0.6

399.597 -6.38

399.731 -12.38

399.865 1.62

399.999 2.62

我想做的是創建兩個列表,其中一個包含例如 [397.451, 397.585, 397.719....等]

和其他 [-48.38, -48.38,-48.38, -18.38,-3.38...等]

堅持基本原則:

fil = open("big_text_file.txt")
list1 = []
list2 = []
text = fil.readline()
while text:
    try:
        nums = text.split()
        list1.append(float(nums[0]))
        list2.append(float(nums[1]))
    except:
        pass
    text = fil.readline()

print(list1)
print(list2)

解釋:

  • 創建兩個列表
  • 正如你所說,這是一個大文本文件(所以逐行閱讀
  • 拆分在空格“”上讀取的行(在split中默認為單個空格)
  • 如果上述失敗意味着空行。 (這就是tryexcept的用途)
  • 更新兩個列表(如果沒有錯誤)
  • 閱讀下一行。

Output:

[397.451, 397.585, 397.719, 397.853, 397.987, 398.121, 398.256, 398.39, 398.524, 398.658, 398.792, 398.926, 399.06, 399.194, 399.328, 399.463, 399.597, 399.731, 399.865, 399.999]
[-48.38, -48.38, -48.38, -18.38, -3.38, 6.62, -0.38, -1.38, 7.62, 4.62, -4.38, 12.62, 5.62, -6.38, -6.38, 0.62, -6.38, -12.38, 1.62, 2.62]

使用 csv 庫: https://docs.python.org/3/library/csv.ZFC35FDC70D5FC69D236988

解決方案:

import csv

with open("spectroscopy.txt", newline="") as csvfile:
    reader = csv.reader(csvfile, delimiter=" ")
    column_A = []
    column_B = []
    for row in reader:
        try:
            column_A.append(float(row[0]))
            column_B.append(float(row[1]))
        except ValueError:
            pass

替代pandas

import pandas as pd

data = pd.read_csv("spectroscopy.txt", sep=" ", header=None, index_col=0)
spect_list = []

spect_list_a =[]

spect_list_b =[]

with open('spect.txt') as f:
    for i in  f.readlines():            #read entire file as lines
        i = (i.rstrip('\n'))        #remove newlin character
        if i:                       #discard blank lines
            spect_list.append(i)
            spect_list_a.append(i.split()[0])
            spect_list_b.append(i.split()[1])
                 
print(spect_list)
print(spect_list_a)
print(spect_list_b)

你得到 python 列表元素為“元素”(帶引號)不確定是正確的答案

知道了:

利用

spect_list_a.append(float(i.split()[0]))
spect_list_b.append(float(i.split()[1]))

使用轉置技巧和參數將列自動轉換為浮動。 此外, skipinitialspace處理幾行,值之間有兩個空格。

import csv

# The quoting value auto-converts numeric columns to float.
with open('input.csv',newline='') as f:
    r = csv.reader(f,delimiter=' ',quoting=csv.QUOTE_NONNUMERIC,skipinitialspace=True)
    data = list(r)

# transpose row/col data and convert to list (otherwise, it would be tuple)
col1,col2 = [list(col) for col in zip(*data)]
print(col1)
print(col2)
[397.451, 397.585, 397.719, 397.853, 397.987, 398.121, 398.256, 398.39, 398.524, 398.658, 398.792, 398.926, 399.06, 399.194, 399.328, 399.463, 399.597, 399.731, 399.865, 399.999]
[-48.38, -48.38, -48.38, -18.38, -3.38, 6.62, -0.38, -1.38, 7.62, 4.62, -4.38, 12.62, 5.62, -6.38, -6.38, 0.62, -6.38, -12.38, 1.62, 2.62]

使用pandas

import pandas as pd
data = pd.read_csv('input.csv',sep=' ',skipinitialspace=True,header=None)
col1 = list(data[0])
col2 = list(data[1])
print(col1)
print(col2)

不使用進口:

with open('input.csv') as f:
    data = [[float(n) for n in row.split()] for row in f]
col1,col2 = [list(n) for n in zip(*data)]
print(col1)
print(col2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM