簡體   English   中英

以特定格式寫入文件?

[英]Writing to file in specific format?

我有一個方法可以生成並返回一個字符串和一個字符串列表。 例子:

def func():
    string_val = some code to generate string...
    list_of_string = some code to generate list of string...
    return string_val, list_of_string

Output:

string_val = 'some string_1'
list_of_string = ['string 1.1', 'string 1.2']

我的方法將被多個線程調用(我正在使用concurrent.futures.thread中的ThreadPoolExecutor )並且每個線程將生成不同的數據。

我想將此數據寫入以下格式的文件:

some string_1   <-- value returned by thread 1
some string_2   <-- value returned by thread 2
.
.
.
some string_n   <-- value returned by thread n

string 1.1   <-- value returned by thread 1
string 1.2   <-- value returned by thread 1
string 2.1   <-- value returned by thread 2
string 2.2   <-- value returned by thread 2
string 2.3   <-- value returned by thread 2
.
.
.
string n.m   <-- value returned by thread n

編輯:

生成並存儲在list_of_string中的值取決於string_val的值。

有沒有實現這一目標的最佳方法?

您可以使用thread_name_prefix為每個線程池分配一個特定的名稱。

你可以看看這個問題和例子

如何給 Python 中的 ThreadPoolExecutor 線程起不同的名稱

暫無
暫無

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

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