我绝不是程序员,但我偶然发现了一个非常讨厌的固定宽度ASCII表,这可能要求我成为一个:)(希望你们有一些帮助)
我已经向谷歌先生提出了一些建议,他指出了我的方向。所以我在这里 - 很丢失:(
违规表格如下:
column1 column2 column3 column4 column5 column6 column7 ... columnN data crap crap data crap crap data data crap crap data crap crap data data crap crap data crap crap data crap crap crap data crap crap data crap crap data data crap crap data crap crap data data crap crap crap crap data data crap crap data crap data data crap crap data crap crap data data crap crap data crap crap data
正如您所看到的,列数可能会有所不同,表中有部分没有数据,还有列中包含我不感兴趣的数据。
我的目标是在最后有一张表,如下所示:
column1 column4 column7 ... columnN data data data data data data data data data data data data data data data data data data data data data data data data data data
所以,现在我不想要的所有列都消失了。这基本上就是我的目标 - 一个只有我感兴趣的列的表。你认为这样的事情可以在Python中完成吗?
答案 0 :(得分:2)
听起来您正在尝试从文本文件中读取表信息,然后重新格式化它。一些基本处理可能如下所示:
# First read content into an array
# Each item in the array will be a line of the file
with open('filename.txt') as f:
content = f.readlines()
# Next, parse each line
data = []
for line in content:
# You might need to split by spaces
# This takes care of multiple whitespaces, so "data1 data2 data3 data4"
# Becomes ['data1','data2','data3','data4']
row = line.split()
# Or, maybe you will need to split the row up by tabs into an array
# [] is a list comprehension, strip() will remove extra whitespace
row = [item.strip() for item in line.split('\t')]
# Finally, append the row to your data array
data.append(row)
# Now, print the data back to a file how you'd like
fout = open('output.txt','w')
for row in data:
# For specific columns
fout.write('{0} {1} {2} {3}'.format(row[0],row[1],row[7],row[8]))
# Or, if you just need to remove a couple columns, you might do:
row.pop(6)
row.pop(5)
row.pop(4)
fout.write(' '.join(row))
答案 1 :(得分:1)
>>> row = " data crap crap crap crap data"
>>> width = 8 # Column width
>>> columns = [row[i*width:(i+1)*width].strip() for i in range((len(row)/width)+1)]
>>> columns
['data', 'crap', 'crap', '', 'crap', 'crap', 'data']
现在你所要做的就是选择你的专栏:
>>> columns[0], columns[3], columns[6]
('data', '', 'data')
我可以想象上面的代码对你来说仍然是胡言乱语;我强烈建议你开始学习编程。 Python是一个优秀的语言,从http://wiki.python.org/moin/BeginnersGuide开始,从那里开始工作!
答案 2 :(得分:0)
虽然我真的认为你应该在python中为了学习python而编写代码,但如果你想要完成它,那就尝试使用Excel!