[英]How to extract data from word to excel when we have different number of lines in different text files
I have many text files to extract data from to excel.我有许多文本文件要从中提取数据到 excel。 Some files have one line into data like:
data: binary\n AAA
.有些文件只有一行数据,例如:
data: binary\n AAA
。 I need the "AAA" part so I use:我需要“AAA”部分,所以我使用:
itemData = item.split("data:")[0].strip()
itemlast = item.split()[-1].strip()
However, other text files have two or three lines.但是,其他文本文件有两行或三行。 My question is: How can I make a loop to start when it finds the word (data), parse everything that comes into it, and stops before the next data line?
我的问题是:如何在找到单词(数据)时开始循环,解析进入其中的所有内容,并在下一个数据行之前停止?
This code seems to work:这段代码似乎工作:
txt = '''
data: binary
AAA
data: binary
AAA
BBB
CCC
data: binary
AAA
ZZZQQ
data: binary
XXX1
xxx2
'''
lst = txt.split('\ndata:')[1:] # use [1:] if first record empty
for s in lst:
print(s.replace('\n','').replace('binary','').strip())
Output Output
AAA
AAA BBB CCC
AAA ZZZQQ
XXX1 xxx2
itemlast = "".join(item.split("data:")[1].replace(" ","").strip().split("\n")[1:])
I put this inside a loop and it works perfectly now!我把它放在一个循环中,它现在可以正常工作了! Thank you!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.