python - 从文件中提取特定行并在 python 中创建数据部分

标签 python

尝试编写一个 python 脚本来从文件中提取行。该文件是一个文本文件,是 python suds 输出的转储。

我想要:

  1. 删除除单词和数字之外的所有字符。我不需要任何“\n”、“[”、“]”、“{”、“=”等字符。
  2. 找到以“ArrayOf_xsd_string”开头的部分
  3. 从结果中删除下一行“item[] =”
  4. 捕获剩下的 6 行,并根据第五行上的唯一数字(123456、234567、345678)创建一个字典,使用该数字作为键,其余行作为值(如果我不是,请原谅我的无知)用Python术语解释这一点)
  5. 将结果输出到文件

文件中的数据是一个列表:

[(ArrayOf_xsd_string){
   item[] = 
      "001",
      "ABCD",
      "1234",
      "wordy type stuff",
      "123456",
      "more stuff, etc",
 }, (ArrayOf_xsd_string){
   item[] = 
      "002",
      "ABCD",
      "1234",
      "wordy type stuff",
      "234567",
      "more stuff, etc",
 }, (ArrayOf_xsd_string){
   item[] = 
      "003",
      "ABCD",
      "1234",
      "wordy type stuff",
      "345678",
      "more stuff, etc",
 }]

我尝试进行重新编译,这是我对代码的糟糕尝试:

import re, string

f = open('data.txt', 'rb')
linelist = []
for line in f:
  line = re.compile('[\W_]+')
 line.sub('', string.printable)
 linelist.append(line)
 print linelist

newlines = []
for line in linelist:
    mylines = line.split()
    if re.search(r'\w+', 'ArrayOf_xsd_string'):
      newlines.append([next(linelist) for _ in range(6)])
      print newlines

我是一个 Python 新手,在 google 或 stackoverflow 上没有找到任何关于如何在找到特定文本后提取特定行数的结果。非常感谢任何帮助。

请忽略我的代码,因为我正在“在黑暗中拍摄”:)

这是我希望看到的结果:

123456: 001,ABCD,1234,wordy type stuff,more stuff etc
234567: 002,ABCD,1234,wordy type stuff,more stuff etc
345678: 003,ABCD,1234,wordy type stuff,more stuff etc

我希望这有助于解释我有缺陷的代码。

最佳答案

关于您的代码的一些建议:

删除所有非字母数字字符是完全没有必要的,而且浪费时间;无需构建linelist。您是否知道可以简单地使用普通的旧 string.find("ArrayOf_xsd_string")re.search(...)

  1. 删除除单词和数字之外的所有字符。我不需要任何“\n”、“[”、“]”、“{”、“=”等字符。
  2. 找到以“ArrayOf_xsd_string”开头的部分
  3. 从结果中删除下一行“item[] =”

那么对于你的正则表达式,_ 已经被 \W 覆盖了。但是以下对行的重新分配会覆盖您刚刚读取的行?

for line in f:
  line = re.compile('[\W_]+') # overwrites the line you just read??
  line.sub('', string.printable)

这是我的版本,它直接读取文件,并且还处理多个匹配:

with open('data.txt', 'r') as f:
    theDict = {}
    found = -1
    for (lineno,line) in enumerate(f):
        if found < 0:
            if line.find('ArrayOf_xsd_string')>=0:
                found = lineno
                entries = []
            continue
        # Grab following 6 lines...
        if 2 <= (lineno-found) <= 6+1:
            entry = line.strip(' ""{}[]=:,')
            entries.append(entry)
        #then create a dict with the key from line 5
        if (lineno-found) == 6+1:
            key = entries.pop(4)
            theDict[key] = entries
            print key, ','.join(entries) # comma-separated, no quotes
            #break # if you want to end on first match
            found = -1 # to process multiple matches

输出正是您想要的(这就是 ','.join(entries) 的用途):

123456 001,ABCD,1234,wordy type stuff,more stuff, etc
234567 002,ABCD,1234,wordy type stuff,more stuff, etc
345678 003,ABCD,1234,wordy type stuff,more stuff, etc

关于python - 从文件中提取特定行并在 python 中创建数据部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7451239/

相关文章:

python - 为什么运行 python 解释器和 python 代码的结果不同?

python - 在 Python 中创建没有变量 "x in"(例如范围)的生成器表达式或列表理解

python - 如何使用 Pandas 类似于 GIS 溶解操作来汇总 Python 中的分段道路数据?

python - 如何使用 Tkinter 根据长度创建彩色线条?

python - 是否应该在 __init__ 中初始化所有成员变量

python - Django - <class> 没有外键

php - 通过代码模拟用户浏览

python - 抓取多个页面时经常出现 HTTP 错误 413

python - argparse 的非常基本的例子?

python - 可以用uwsgi协议(protocol)调用http吗?