簡體   English   中英

如何在 python 中將一對空格寫入文本文件?

[英]how to write a pair space delimited to a text file in python?

我有一個空格分隔的字符串,例如:

-50. 2320.034873495636 -37.028592075933 2320.742117324367 -27.833419404448 2320.329844569581 -26.805460276239 2320.302239847568 -26.661859170481 2320.383218590322 -26.369639786391 2320.285153586396 -20.533526952571 2320.307048549957 -18.430372069319 2320.322131347984 -17.682267511693 2320.183044082935 -9.412365074428 2318.640728018348 -4.39105022394 2318.702274658016 -2.799896950857 2319.050216505886 -1.650828549365 2318.851729275538 0.000000000007 2318.821619902524 0.924165106416 2318.804764104907 4.836725761028 2319.15202498789 7.95861459112 2319.5814968401 9.681527521478 2320.446202236106 16.837024777724 2320.776558542595 22.698760541826 2320.670478882793 28.2917471128 2320.68119460127 34.902349455571 2320.270498276339 41.416761759252 2319.105567597744 46.219145248796 2318.698663233488 50.000000000015 2318.45911198918

在一個變量中。 我想將其導出到一個文本文件,如:

-50 2320.034873495636
-37.028592075933 2320.742117324367
-27.833419404448 2320.329844569581
.
.
.

您可以拆分數據,然后在結果列表上使用迭代器,讓您進行迭代並獲取循環中的下一個值:

data = '-50. 2320.034873495636 -37.028592075933 2320.742117324367'
values = iter(data.split())

with open('outfile.txt', 'w') as f:
    for val in values:
        f.write(f'{val} {next(values)}\n')

文件內容:

-50. 2320.034873495636
-37.028592075933 2320.742117324367

當然,如果你的數據無效並且你有奇數個值,你會得到一個錯誤( StopIteration )。


如果您首先有一個迭代器,那么第一種方法真的很有趣。 由於我們有一個列表,一個更經典的方法可能是 zip 具有偶數索引的項目列表和具有奇數索引的項目列表:

data = '-50. 2320.034873495636 -37.028592075933 2320.742117324367 6'
values = data.split()

with open('outfile.txt', 'w') as f:
    for val1, val2 in zip(values[::2], values[1::2]):
        f.write(f'{val1} {val2}\n')

這給出了相同的 output。

您可以在列表中實現列表。

首先,您用“-”分隔每一對。 創建此列表后,您可以分隔列表的每個元素(在您的情況下將是坐標對),您可以使用空格縮進。

所以回顧一下。

1) 使用“-”作為分隔符拆分整個列表。 2) 使用空格分割列表的元素。

我在這里沒有包含任何代碼示例,因為我鼓勵您嘗試通過使用在 python2 和 python3 中實現的 strip() 和 split() 函數來自己實現它。

使用.split()將字符串轉換為一維列表,然后遍歷所有其他元素並將它們成對寫入文件。

inpstr = "-50. 2320.034873495636 -37.028592075933 2320.742117324367 -27.833419404448 2320.329844569581 -26.805460276239 2320.302239847568 -26.661859170481 2320.383218590322 -26.369639786391 2320.285153586396 -20.533526952571 2320.307048549957 -18.430372069319 2320.322131347984 -17.682267511693 2320.183044082935 -9.412365074428 2318.640728018348 -4.39105022394 2318.702274658016 -2.799896950857 2319.050216505886 -1.650828549365 2318.851729275538 0.000000000007 2318.821619902524 0.924165106416 2318.804764104907 4.836725761028 2319.15202498789 7.95861459112 2319.5814968401 9.681527521478 2320.446202236106 16.837024777724 2320.776558542595 22.698760541826 2320.670478882793 28.2917471128 2320.68119460127 34.902349455571 2320.270498276339 41.416761759252 2319.105567597744 46.219145248796 2318.698663233488 50.000000000015 2318.45911198918"
inplst = inpstr.split(" ")
with open("outputfile.txt", "w") as outfile:
  for i in range(0, len(inplst), 2):
    outfile.write(f"{float(inplst[i])} {float(inplst[i + 1])}\n")

如果您的inpstr有奇數個數字,這可能會產生IndexError

你可以這樣做:

_str = "50. 2320.034873495636 -37.028592075933 2320.742117324367 -27.833419404448 2320.329844569581 -26.805460276239 2320.302239847568 -26.661859170481 2320.383218590322 -26.369639786391 2320.285153586396 -20.533526952571 2320.307048549957 -18.430372069319 2320.322131347984 -17.682267511693 2320.183044082935 -9.412365074428 2318.640728018348 -4.39105022394 2318.702274658016 -2.799896950857 2319.050216505886 -1.650828549365 2318.851729275538 0.000000000007 2318.821619902524 0.924165106416 2318.804764104907 4.836725761028 2319.15202498789 7.95861459112 2319.5814968401 9.681527521478 2320.446202236106 16.837024777724 2320.776558542595 22.698760541826 2320.670478882793 28.2917471128 2320.68119460127 34.902349455571 2320.270498276339 41.416761759252 2319.105567597744 46.219145248796 2318.698663233488 50.000000000015 2318.45911198918"
_list = _str.split()
pair_list = []
for i in range(0, len(_list), 2):
    pair_list.append(' '.join(_list[i:i+2]))
with open('pairs.txt', 'w') as f:
    for item in pair_list:
        f.write("{}\n".format(item))

您可以使用zip ,如下所示:

fin = "-50. 2320.034873495636 -37.028592075933 2320.742117324367 -27.833419404448 2320.329844569581 -26.805460276239 2320.302239847568 -26.661859170481 2320.383218590322 -26.369639786391 2320.285153586396 -20.533526952571 2320.307048549957 -18.430372069319 2320.322131347984 -17.682267511693 2320.183044082935 -9.412365074428 2318.640728018348 -4.39105022394 2318.702274658016 -2.799896950857 2319.050216505886 -1.650828549365 2318.851729275538 0.000000000007 2318.821619902524 0.924165106416 2318.804764104907 4.836725761028 2319.15202498789 7.95861459112 2319.5814968401 9.681527521478 2320.446202236106 16.837024777724 2320.776558542595 22.698760541826 2320.670478882793 28.2917471128 2320.68119460127 34.902349455571 2320.270498276339 41.416761759252 2319.105567597744 46.219145248796 2318.698663233488 50.000000000015 2318.45911198918"

fout = "\n".join(f"{x} {y}"
                 for splitted in [fin.split(" ")]
                 for x, y in zip(splitted, splitted[1:]))
print(fout)

哪個產量

-50. 2320.034873495636
2320.034873495636 -37.028592075933
-37.028592075933 2320.742117324367
2320.742117324367 -27.833419404448
...

暫無
暫無

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

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