简体   繁体   English

当我们在不同的文本文件中有不同的行数时,如何从 word 中提取数据到 excel

[英]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.

相关问题 如何使用python3.6.4在两个文本文件中提取不同行的word文本并输出 - how to extract different lines word text and output in two text files using python3.6.4 如何将一列中基于不同类别的数据行提取到单独的文本文件中? - How can I extract lines of data based on different categories in one column into separate text files? 比较2个文本文件中具有不同列数的行 - Compare lines in 2 text files with different number of columns 如何从 python 中的文件中提取不同行的某些数据 - How to extract certain data on different lines from file in python 比较2个文件并提取不同的行 - Compare 2 files and extract lines that are different 如何根据这些文本文件中的行数将某些文本文件传输到不同的目录? - How to transfer certain text files to a different directory based on the number of lines within these text files? 提取在不同文本文件中更改的特定行 - Extract specific lines which changes in different text files 如何从包含在不同文件夹中的 docx 文件中提取文本 - how to extract text from docx files contaning in different folders 如何从 pandas 系列中提取具有不同字母数据的数字? - How to extract number with different alphabet data from pandas serie? Python:比较两个文本文件中具有不同列数的部分行 - Python: comparing part of lines in two text files with different number of columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM