Hi, I'm new to Python and have a task of reading a user input text file that is tab-delimited and contains 4 columns in each line: Authors, Year, Title and Journal.

I currently am just able to open a file, and now I don't know how to begin parsing the data.

The recommended way of sorting the data is to use the following three lists (which I set as):

authorsList = []
journalsList = []
papersList = []

In the papersList, each paper's entry is its title, year published, the index of each author(s) and the index of the journal; in this way the name of each journal and author is only stored in one place.

What I learned to do in Python: basic I/O, loops and conditions, defining functions and little exception handling. I've been going through google but a lot of answers to the same question I have, have been using the csv module and regular expressions, which I tried to learn myself but couldn't understand the code that was suggested. Is there a way to do it without the csv and re module?

I was thinking of doing something like this:

for line in openfile:
   a, b, c, d = line.split("\t")
   authorsList.append(a)
   papersList.append(b, c)
   journalsList.append(d)

but dont think that is right at all.
Any suggestions or tips?
Thanks for your time and consideration.

i'm not quite sure exactly what you mean :D sorry
Well, the text file contains data that looks like this:

AUTHOR(S) YEAR TITLE JOURNAL/CONFERENCE
Accot;Zhai 2001 Scale effects in steering law tasks Proc. ACM CHI
Acredolo 1977 Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space Developmental Psychology
Aginsky;Harris;Rensink;Beusmans 1997 Two strategies for learning a route in a driving simulator Journal of Environmental Psychology

And the entire program function is for a user to input a text file, and then be able to search for papers either by author or journal with the following suggested prompt:

Search an author (A = ***) or journal/conference (J = ***), where *** is any string [Q = quit]:

and then the output of the search will look like this:

Acredolo. (1977). Developmental Changes in Large-Scale Space. Developmental Psychology.

Allen & Ondracek. (1995). Age-sensitive cognitive abilities related to children's acquisition of spatial knowledge. Developmental Psychology.

Aginsky, Harris & Beusmans. (1997). Two strategies for learning a route. Journal of Environmental Psychology.
Submission.

where multiple author names are separated by "&" or ","

hope that answers your question :D
thanks for replying!

While waiting for replies, I have been working on the program and this is what I have so far:

from string import *

while True:
   inputfile = raw_input("Input filename: ")
   try:
      openfile = open(inputfile, "r")
      break
   except:
      print "Invalid file.  Please enter a correct file."

tempAuthorsList = []
journalsList = []
authorsList = []
papersList = []

for line in openfile:
   authors, year, title, journal = line.split("\t")
   papersList.append(year)
   journalsList.append(journal)
   tempAuthorsList.append(authors)

for line in tempAuthorsList:
   names = line.split(";")
   authorsList.append(names)

def authorNameView(terms):
   for term in terms:
      if "," in term:
         sepchar = "& "
         break
   else:
      sepchar = ", "
   
   n = len(terms)
   if n == 0:
      return ""
   elif n == 1:
      return terms[0]
   elif n == 2:
      return " & ".join(terms)
   return "%s%s& %s" % (sepchar.join(terms[:-1]), sepchar, terms[-1])

openfile.close()

print "Search an author (A = ***) or journal/conference (J = ***), where *** is any string [Q = quit]:"

I was able to parse the data to tabs, but I don't know how to append the papersList with both the title and the year, since I was getting an error when I tried to do this:

papersList.append(year, title)

The error said append() only takes one argument

Mayby you need to do one of these:

paperList.append(year).append(title)
paperList.append([year,title])
paperList.extend([year,title])

i'm not quite sure exactly what you mean :D sorry
Well, the text file contains data that looks like this:

And the entire program function is for a user to input a text file, and then be able to search for papers either by author or journal with the following suggested prompt:

Can you resend the input in code tags or attach from "Additional Options -> Attach files" an input file

Can you resend the input in code tags or attach from "Additional Options -> Attach files" an input file
a3-example-data.txt

a3-example-error1.txt

The first file is a correct file
and I am also supposed to be able to error handle incomplete files as the second one is.

Attachments
AUTHOR(S)	YEAR	TITLE	JOURNAL/CONFERENCE
Accot;Zhai	2001	Scale effects in steering law tasks	Proc. ACM CHI
Acredolo	1977	Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space	Developmental Psychology
Aginsky;Harris;Rensink;Beusmans	1997	Two strategies for learning a route in a driving simulator	Journal of Environmental Psychology
Agrawala;Beers;Frohlich;Hanrahan;McDowall;Bolas	1997	The two-user responsive workbench: Support for collaboration through individual views of a shared space	Proc. ACM SIGGRAPH
Ahmadabadi;Eiji	1996	Cooperation strategy for a group of object lifting robots	Proc. of IROS
Ahuja;Webster	2001	Perceived disorientation: An examination of a new measure to assess web design effectiveness	Interacting with Computers
Albers;Bergman	1995	The audible Web: Auditory enhancements for Mosaic	Proc. ACM CHI
Aldestein;Ellis	1993	Effect of Head-Slaved Visual Image Roll on Spatial Situation Awareness	Proc. HFES
Alexander;Wickens	2001	"Cockpit display of traffic information: The effects of traffic load, dimensionality, and vertical profile orientation"	Proc. HFES
Alfano;Michel	1990	Restricting the field of view: perceptual and performance effects	Perceptual and Motor Skills
Allen;Ondracek	1995	Age-sensitive cognitive abilities related to children's acquisition of spatial knowledge	Developmental Psychology
Allen;Siegel;Roninski	1978	The Role of Perceptual Context in Structuring Spatial Knowledge	Journal of Experimental Psychology: Human Learning and Memory
Allen;Singer	1997	Landmark detection and distance estimation in large scale virtual environments	Proc. HFES
Amazeen;Turvey	1996	Weight perception and the haptic size-weight illusion are functions of the inertia tensor	Journal of Experimental Psychology: Human Perception and Performance
Angelaki;Shaikh;Green;Dickman	2004	Neurons compute internal models of the physical laws of motion	Nature
Anonymous	1993	Technical correspondance - Random Number Generators	Communications of the ACM
Anonymous	1992	Research Directions in Virtual Environments (NSF Invitational Workshop)	Computer Graphics
Anonymous	1998	"Exploring sequential data: Commentary on Bowers, Jentsch, Salas, and Braun (1998)"	Human Factors
Arai;Ota	1996	Let us work together - Task planning of multiple mobile robots	Intelligent Robots and Systems
Aretz	1991	The Design of Electronic Map Displays	Human Factors
Arnold;Farrell;Pettifer;West	2002	Performance of a skilled motor task in virtual and real environments	Ergonomics
Arthur;Booth;Ware	1993	Evaluating 3D Task Performance for Fish Tank Virtual Worlds	ACM Transactions on Information Systems
Authier;Lortie;Gagnon	1996	Manual handling techniques: Comparing novices and experts	International Journal of Industrial Ergonomics
Authur;Hancock;Chrysler	1993	Spatial orientation in Real and Virtual Worlds	Proc. HFES
Avraamides;Klatzky;Loomis;Golledge	2004	use of cognitive versus perceptual heading during imagined locomotion depends on the response mode	Psychological Science
Aytes	1996	Comparing collaborative drawing tools and whiteboards: An analysis of the group process	Computer Supported Cooperative Work
Badler;Hollick;Granieri	1993	Real-time control of a virtual human using minimal sensors	Presence: Teleoperators and Virtual Environments
Badler;Palmer;Bindiganavale	1999	Animation control for real-time virtual humans	Communications of the ACM
Bailenson;Blascovich;Beall;Loomis	2001	Equilibrium theory revisited: Mutual gaze and personal space in virtual environments	Presence: Teleoperators and Virtual Environments
Baird;Wagner;Noma	1982	Impossible cognitive spaces	Geographical Analysis
Bajura;Fuchs;Ohbuchi	1992	Merging virtual objects with the real world: Seeing ultrasound imagery within the patient	Proc. ACM SIGGRAPH
Bakker;Werkhoven;Passenier	1999	The effects of proprioceptive and visual feedback on geographical orientation in virtual environments	Presence: Teleoperators and Virtual Environments
Bakker;Werkhoven;Passenier	2001	Calibrating visual path integration in VEs	Presence: Teleoperators and Virtual Environments
Balakrishnan;Hinckley	2000	Symmetric bimanual interaction	Proc. ACM CHI
Balakrishnan;Kurtenbach	1999	Exploring bimanual camera control and object manipulation in 3D graphics interfaces	Proc. ACM CHI
Balcisoy;Thalmann	1998	Hybrid approaches to interactions between real and virtual humans in mixed environments	Proc. EGVE
Banks	1992	Interactive Manipulation and Display of Two-Dimensional Surfaces in Four- Dimensional Space	Proc. ACM I3D
Banon	1990	Implementation and extension of the ladder algorithm	Proc. IEEE International Conference on Robotics and Automation
Bardram	2000	Temporal coordination: On time and coordination of collaborative activities at a surgical department	Computer Supported Cooperative Work
Bardy;Oullier;Bootsma;Stoffregen	2002	Dynamics of human postural transitions	Journal of Experimental Psychology: Human Perception and Performance
AUTHOR(S)	YEAR	TITLE	JOURNAL/CONFERENCE
Accot;Zhai	2001	Scale effects in steering law tasks	Proc. ACM CHI
Acredolo	Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space	Developmental Psychology	
Aginsky;Harris;Rensink;Beusmans	1997	Two strategies for learning a route in a driving simulator	Journal of Environmental Psychology
Agrawala;Beers;Frohlich;Hanrahan;McDowall;Bolas	1997	The two-user responsive workbench: Support for collaboration through individual views of a shared space	Proc. ACM SIGGRAPH
Ahmadabadi;Eiji	1996	Cooperation strategy for a group of object lifting robots	Proc. of IROS

Here small error check for main format of the file

def notfourrecords(a):
    return a.count('\t') != 3

notfound=True
while notfound:
   inputfile = raw_input("Input filename: ")
   try:
      openfile = open(inputfile, "r")
      errors = [i for i in openfile if notfourrecords(i)]
      if errors:
         print "Incorrect record format in records: \n"
         for i in errors: print i
         raise ValueError
      openfile.close()
      openfile = open(inputfile, "r")
      notfound=False

   except:
      print "Invalid file.  Please enter a correct file."
      notfound=True
input filename: a3-example-error1.txt
Incorrect record format in records: 

Acredolo	Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space	Developmental Psychology

Invalid file.  Please enter a correct file.
Input filename:
This question has already been answered. Start a new discussion instead.