How to use python to read a text file into a csv by picking certain parts

python csv read specific row and column
python read csv line by line
python read csv column into list
python read csv example
how to extract data from csv file in python
python read csv into list
csv reader python
python read csv into variables

I have a text file with looks like this:

Current job title: meter engineer Current salary: £30,000 Experience: 2 years Desired location: Not supplied Desired job title: smart meter engineer Desired salary: £30,000 Job Type: Permanent | Contract | Temp

Current job title: dual fuel smart meter engineer Current salary: £30,000 Experience: 4 years Desired location: Not supplied Desired job title: Not supplied Desired salary: £34,999 Job Type: Permanent | Contract | Temp

each line is split with a new line and the sets of data is separated by blank space I want to use python to extract the data under the headings. eg: Current job title: meter engineer would go in the Current job title column. Then grab the next set and put those on the next line

how do I achieve this using python?

I am new to python. All I can get it to do is read the file. Picking out the data using IF doesn't work.

 f = open("test.txt", "r")
 lines = f.readlines()
 for line in lines:

import csv

with open('test.csv', 'rU') as infile:
  reader = csv.DictReader(infile)
  data = {}
  for row in reader:
    for header, value in row.items():
      except KeyError:
        data[header] = [value]

How to read and write a CSV files with Python, Use Python to read and write comma-delimited files. Using The csv module in Python can be used to quickly parse CSV files into the user for a file to import filter = "CSV file (*.csv)|*.csv|*.txt|All Files (*. GetPoints(draw_lines=False, message1="Pick points to save to CSV:") if( points==None): return with  Here's what it would like so far: import csv f = open ('attendees1.csv') csv_f = csv.reader (f) To run this program, in your terminal, go into the directory containing the Python program file and the attendees CSV files, and type python

You'll first need to restructure your data before converting it to a csv format.

Try this:

import csv
from collections import OrderedDict

with open('data.txt', 'r') as data, open('output.csv', 'w') as file:
  rows ='\n\n')
  output = [
      (k.rstrip(':'), v) for k, v in zip(row.split('\n')[::2], row.split('\n')[1::2])
    for row in rows
  writer = csv.DictWriter(file, fieldnames=output[0].keys(), lineterminator='\n')

Repl sample


Current job title,Current salary,Experience,Desired location,Desired job title,Desired salary,Job Type
meter engineer,"£30,000",2 years,Not supplied,smart meter engineer,"£30,000",Permanent | Contract | Temp
dual fuel smart meter engineer,"£30,000",4 years,Not supplied,Not supplied,"£34,999",Permanent | Contract | Temp

Note you need to use OrderedDict as dictionary objects are not ordered on python 2.7 and will cause trouble for your csv file. Relevant pseudo OrderedDict comprehension was inspired by this answer here: Is there an OrderedDict comprehension?

As a side note - Python 2.7 is sunsetting. You should really consider moving your project to a Python 3.x base.

Python for Beginners: Reading & Manipulating CSV Files, This tutorial is designed for anyone who is interested in Python, with little to no Opening a CSV file; Looping through rows; Extracting information from a CSV file Python allows you to open text files such as these and read their content, from the list, and the second line (indented, since we want this line to be part of the  Converting simple text file without formatting to dataframe can be done by (which one to chose depends on your data): pandas.read_fwf - Read a table of fixed-width formatted lines into DataFrame pandas.read_fwf (filepath_or_buffer, colspecs='infer', widths=None, **kwds) pandas.read_csv - Read CSV (comma-separated) file into DataFrame.

This Code is basic but it might just do the trick. All it does is read the line that is found as the one with the data on it. It knows which line is on because its assume its this example exactly.

 f = open("test.txt", "r")

if f.mode == 'r': 

  repeat = f.find('Current job title:') #this give how many times 'Current job title is found in the program'

  while repeat >= 1:

    repeat = repeat - 1 #changes repeat by -1 every time you do this counting how many times you run this loop

    print("Job Title:")          #Finds and sets JobTitle to the title of job stated
    JobTitle = f.readline(repeat * 15 - 13)

    print(JobTitle)   #Prints Job Title

    print("Current salary:")            #Repeat from Above
    Pay = f.readline(repeat * 15 - 11)

Extract, Transform, and Save CSV data •, This tutorial explains how to extract place names from a CSV file, clean them up a bit, and save them to a regular text file using python. Let's use the csv module, which we can import at the beginning of the file, and use to read in the CSV file. We don't want our i+=1 code to part of the if block, or it will never run! You can  To skip reading the whole file and read a specific line, the lines must be fixed length. How else to know what line you are on without counting every newline in the file up to the line you want, and that means reading the whole file from the beginning. CSV is the wrong format for a 20gb file. Convert it to a database.

Reading and Writing CSV Files in Python – Real Python, Learn how to read, process, and parse CSV from text files using Python. but it is a key part of the Anaconda distribution and works extremely well in Jupyter  A Python program can read a text file using the built-in open() function. For example, below is a Python 3 program that opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and then prints the data.

Python for Beginners: Reading & Manipulating CSV Files, A CSV file is a human readable text file where each line has a number of fields, separated by You can also write CSV files using the csv module. There are many websites that provide interesting information in CSV format. This article explains how to load and parse a CSV file in Python. First of all, what is a CSV ? CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record.

Python CSV Files: Reading and Writing, If you find this information useful, consider picking up a copy of my book, The Python Standard Library By Example. Use reader() to create a an object for reading data from a CSV file. The reader can be used as an iterator to process the rows of the file in order. The first argument to reader() is the source of text lines. In  The Postgres command to load files directy into tables is called COPY. It takes in a file (like a CSV) and automatically loads the file into a Postgres table. Instead of creating the query and then running it through execute() like INSERT, psycopg2, has a method written solely for this query. The method to load a file into a table is called