許多帶有 .txt 擴展名的文本檔案存在于目錄(1620_10.asc_rsmp_1.0.txt, 132_10.asc_rsmp_1.0.txt等)中,檔案名的前幾位是唯一的更改(例如1620在第一個檔案和132第二個檔案中)。我想對text檔案執行一些操作。
每個文本檔案的第一行是一個字串,其余的是浮點數。
step1:我要做的第一件事是從所有現有文本檔案中洗掉第一行
step2:我想將所有文本檔案中的行轉換為列。
step:3之后,我想根據檔案的名稱(132_10.asc_rsmp_1.0.txt 1620_10.asc_rsmp_1.0.txt ...)并排排列步驟2中生成的檔案,并希望保存在單獨的檔案中。
cat 1620_10.asc_rsmp_1.0.txt
TIMESERIES ____, 5605 xxxxxxx, 1 yyy, 1969-11-31T22:52:10.000000, ZZZZZ, FLOAT,
0.0000000000e 00 5.8895751219e-02 1.9720949872e-02 4.7712552071e-02 1.6255806150e-02 5.0983512543e-02
2.4151940813e-02 4.3959767187e-02 1.9066090517e-02 4.8980189213e-02 2.6237709462e-02 4.1379166269e-02
cat 132_10.asc_rsmp_1.0.txt
TIMESERIES ____, 5605 xxxxxxx, 1 yyy, 1980-12-31T23:58:20.000000, ZZZZZ, FLOAT,
2.0337053383e-02 4.7575540537e-02 2.7508078190e-02 3.9923797852e-02 2.1663353231e-02 4.6368790709e-02
2.8194768989e-02 3.8577115641e-02 2.1935380223e-02 4.6024962357e-02 2.9320681307e-02 3.7630711188e-02
預期輸出:cat output.txt
2.0337053383e-02 0.0000000000e 00
4.7575540537e-02 5.8895751219e-02
2.7508078190e-02 1.9720949872e-02
3.9923797852e-02 4.7712552071e-02
2.1663353231e-02 1.6255806150e-02
4.6368790709e-02 5.0983512543e-02
2.8194768989e-02 2.4151940813e-02
3.8577115641e-02 4.3959767187e-02
2.1935380223e-02 1.9066090517e-02
4.6024962357e-02 4.8980189213e-02
2.9320681307e-02 2.6237709462e-02
3.7630711188e-02 4.1379166269e-02
我的試用碼:
with open("*.txt",'r') as f:
with open("new_file.txt",'w') as f1:
f.next() # skip header line
for line in f:
f1.write(line)
但是它不會產生任何預期的輸出。希望專家提供幫助。謝謝。
uj5u.com熱心網友回復:
目前還不清楚你想要什么。這就是我認為你想要的:
from glob import glob
# Returns a list of all relevant filenames
filenames = glob("*_10.asc_rsmp_1.0.txt")
# All the values will be stored in a dict where the key is the filename, and
# the value is a list of values
# It will be used later on to arrange the values side by side
values_by_filename = {}
# Read each filename
for filename in filenames:
with open(filename) as f:
with open(filename "_processed.txt", "w") as f_new:
# Skip the first line (header)
next(f)
# Add all the values on every line to a single list
values = []
for line in f:
values.extend(line.split())
# Write each value on a new line in a new file
f_new.write("\n".join(values))
# Store the original filename and values to a dict for later
values_by_filename[filename] = values
# Order the filenames by the number before the first underscore
ordered_filenames = sorted(values_by_filename,
key=lambda filename: int(filename.split("_")[0]))
# Arrange the values side by side in a new file
# zip iterates over every list of values at once, yielding the next value
# from every list as a tuple each iteration
lines = []
for values in zip(*(values_by_filename[filename] for filename in ordered_filenames)):
# Separate each column by 3 spaces, as per your expected output
lines.append(" ".join(values))
# Write the concatenated values to file with a newline between each row, but
# not at the end of the file
with open("output.txt", "w") as f:
f.write("\n".join(lines))
output.txt:
2.0337053383e-02 0.0000000000e 00
4.7575540537e-02 5.8895751219e-02
2.7508078190e-02 1.9720949872e-02
3.9923797852e-02 4.7712552071e-02
2.1663353231e-02 1.6255806150e-02
4.6368790709e-02 5.0983512543e-02
2.8194768989e-02 2.4151940813e-02
3.8577115641e-02 4.3959767187e-02
2.1935380223e-02 1.9066090517e-02
4.6024962357e-02 4.8980189213e-02
2.9320681307e-02 2.6237709462e-02
3.7630711188e-02 4.1379166269e-02
請務必閱讀檔案,尤其是:
- https://docs.python.org/3/library/glob.html#glob.glob
- https://docs.python.org/3/library/functions.html#zip
- https://docs.python.org/3/library/functions.html#sorted
- https://docs.python.org/3/library/stdtypes.html#str.join
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497777.html
標籤:Python python-3.x 数据框 麻木的 球体
上一篇:在Matplotlib中標注點
下一篇:洗掉二維陣列中的重復索引
