Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read csv on python with newline separator @

Tags:

python

csv

I need to read a csv on Python, and the text file that I have has this structure:

"114555","CM13","0004","0","C/U"@"99172","CM13","0001","0","C/U"@"178672","CM13","0001","0","C/U"

delimeter: ,

newline: @

My code so far:

import csv
data = []
with open('stock.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', lineterminator='@')
    for row in reader:
        data.append({'MATERIAL':  row[0],'CENTRO': row[1], 'ALMACEN': row[2], 'STOCK_VALORIZADO' : row[3], 'STOCK_UMB':row[4]})


print(data)  #this print just one row

This code only print one row, because it's not recognize @ as a newline, and prints it with quotes:

[{'MATERIAL': '114555', 'CENTRO': 'CM13', 'ALMACEN': '0004', 'STOCK_VALORIZADO': '0', 'STOCK_UMB': 'C/U@"99172"'}]
like image 399
Marcelo Avatar asked Apr 27 '15 18:04

Marcelo


People also ask

How do I read a CSV file in a different separator?

Adding "sep=;" or "sep=," to the CSV file Here are the steps you should follow: Open your CSV using a text editor. Skip a line at the top, and add sep=; if the separator used in the CSV is a semicolon (;), or sep=, if the separator is a comma (,). Save, and re-open the file.

How do I handle a new line character in a CSV file?

Specifying and Saving Newline Characters Open a CSV file in the text editor. Click Save As from the menu. Specify "CR+LF" or "LF" as a newline character and save the file.


1 Answers

According to https://docs.python.org/2/library/csv.html : "The reader is hard-coded to recognise either '\r' or '\n' as end-of-line, and ignores lineterminator. This behavior may change in the future." Hence for now, providing the argument lineterminator='@' will not work.

I think the best option is to read your entire file into a variable, and replace all '@' characters, you can do this as follows:

with open("stock.csv", "r") as myfile:
    data = myfile.read().replace('@', '\n')

Now you need to adjust your algorithm in such a way that you can pass the variable data to csv.reader (instead of the file stock.csv), according to the python doc:

"The "iterable" argument can be any object that returns a line of input for each iteration, such as a file object or a list. [...]"

Hence you can pass data.splitlines() to csv.reader.

like image 79
Gio Avatar answered Sep 28 '22 00:09

Gio