2
XlsxWriter를 사용하여 Excel 열에 데이터를 쓰고 싶습니다. 각 반복마다 하나의 '데이터'세트가 작성됩니다. 각 세트는 별도의 열에 작성해야합니다. 어떻게해야합니까? [1]
에서XlsxWriter의 각 반복마다 새 열을 만들 수 있습니까?
- I는 루프 밖에
i=0
정의 이상1
하여 증가하고col=i
을 설정
I는 다음과 같이col
값 놀 시도했다. 이 작업이 완료되면 출력이 비어 있습니다. 나에게 이것은 가장 논리적 인 해결책이다. & 왜 작동하지 않을지 모르겠다. - 에서
[2]
i
은 루프 내부에서 정의됩니다. 이런 일이 발생하면 한 개의 열이 쓰여집니다. [3]
에서 표준 방법으로col
을 정의합니다. 이것은 예상대로 작동합니다 : 하나의 열에 쓰여집니다.
내 코드 :
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