简体   繁体   中英

How do I remove \n when making a list from csv?

All I'm trying to do is make a list of attributes without newline \\n at the end of each record.

import os, csv

output = os.system('hive -S -e "use precache; select geode from database WHERE geodetype = 17;" > testausnzl.csv')


f = open('testausnzl.txt').read()

print f 

ausnzl_list = open("testausnzl.csv").readlines()

Instead of reinventing the wheel, use the csv module to do the reading:

with open("testausnzl.csv", 'rb') as f:
    ausnzl_list = list(csv.reader(f))

This will remove the newline characters and produce rows (split on commas).

Instead of a temporary filename, you could use have Python read from the command directly:

import csv
import subprocess

process = subprocess.Popen(
    ['hive', '-S', '-e', 
     '"use precache; select geode from database WHERE geodetype = 17;"'],
    buffer=1, stdout=subprocess.PIPE)
reader = csv.reader(process.stdout)
for row in reader:
    # process each row as you receive it

Instead of loading everything into a list, here the code just loops over the csv.reader() object, to process each row as it comes in instead. That is usually the better and more memory efficient approach.

The best approach would be to use a Python client to connect to hive and not use the command line client at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM