2
XlsxWriterを使用してExcelの列にデータを書きたいと思います。各反復ごとに1つの「セット」のデータが書き込まれます。各セットは別々の列に記載する必要があります。これはどうすればいいですか?次のように私はcol
値で遊んで試してみました
:[1]
でXlsxWriterの反復ごとに新しい列を作成することは可能ですか?
- を私はループの外
i=0
を定義し、後で1
ことによってそれをインクリメントし、col=i
を設定します。これが行われると、出力はブランクになります。私には、これは最も論理的な解決策です。&なぜ動作しないのか分かりません。 [2]
i
がループ内で定義されています。これが起こると、1つの列が書き込まれます。[3]
で標準的な方法でcol
を定義します。期待どおりに動作します.1つの列が書き込まれます。
マイコード:
import xlsxwriter
txt_file = open('folder/txt_file','r')
lines = dofile.readlines()
# [1]Define i outside the loop. When this is used output is blank.
i = 0
for line in lines:
if condition_a is met:
#parse text file to find a string. reg_name = string_1.
elif condition_b:
#parse text file for a second string. esto_name = string_2.
elif condition_c:
#parse text file for a group of strings.
# use .split() to append these strings to a list.
# reg_vars = list of strings.
#[2] Define i inside the loop. When this is used one column gets written. Relevant for [1] & [2].
i+=1 #Increment for each loop
row=1
col=i #Increments by one for each iteration, changing the column.
#[3] #Define col =1. When this is used one column also gets written.
col=1
#Open Excel
book= xlsxwriter.Workbook('folder/variable_list.xlsx')
sheet = book.add_worksheet()
#Write reg_name
sheet.write(row, col, reg_name)
row+=1
#Write esto_name
sheet.write(row, col, esto_name)
row+=1
#Write variables
for variable in reg_vars:
row+=1
sheet.write(row, col, variable)
book.close()
ええと、それは確かに列を通して繰り返しを解決するか? –
上記のアップデートをご覧ください。 – jmcnamara