Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'true,': 0.04; 'subject:Python': 0.05; 'sys': 0.05; '__name__': 0.07; 'extracted': 0.07; 'main()': 0.07; 'assigning': 0.09; 'def': 0.13; 'result.': 0.15; '"":': 0.16; "'__main__':": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'green,': 0.16; 'main():': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'messy': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'row': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.16; 'rid': 0.22; 'split': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'example': 0.26; 'command': 0.26; 'executing': 0.27; 'pieces': 0.27; 'indentation': 0.29; 'print': 0.30; 'code': 0.30; 'table': 0.32; 'received:84': 0.32; 'getting': 0.33; "skip:' 20": 0.34; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'missing': 0.37; 'pdf': 0.37; 'test': 0.39; 'data': 0.39; 'sure': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'email addr:gmail.com': 0.62; 'color': 0.67; 'messed': 0.84; 'subject:via': 0.84; 'yellow': 0.84; '1997': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=JAI3OqB5mnwA:10 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=6kOTikIeVIPAk1p8iVwA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Subject: Re: Concatenating columns via Python To: python-list@python.org References: From: MRAB Date: Tue, 28 Jul 2015 20:17:15 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438111039 news.xs4all.nl 2880 [2001:888:2000:d::a6]:55549 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94705 On 2015-07-28 19:50, hannahgracemcdonald16@gmail.com wrote: > I extracted a table from a PDF so the data is quite messy and the data that should be in 1 row is in 3 colums, like so: > year color location > 1 1997 blue, MD > 2 green, > 3 and yellow > > SO far my code is below, but I know I am missing data I am just not sure what to put in it: > > # Simply read and split an example Table 4 > import sys > The indentation is messed up, which makes it hard to follow. > # Assigning count number and getting rid of right space > def main(): > count = 0 > pieces = [] > for line in open(infile, 'U'): > if count < 130: > data = line.replace('"', '').rstrip().split("\t") > data = clean_data(data) > if data[1] == "year" and data[1] != "": If the first test is true, then the second test is definitely true, and is unnecessary. > write_pieces(pieces) > pieces = data > str.join(pieces) str.join _returns_ its result. > else: > for i in range(len(data)): > pieces[i] = pieces[i] + data[i] > str.join(pieces) > > # Executing command to remove right space > def clean_data(s): > return [x.rstrip() for x in s] > > def write_pieces(pieces): > print > > if __name__ == '__main__': > infile = "file.txt" > main() >