c*9 发帖数: 3241 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: peking2 (Lambda), 信区: Programming
标 题: 怎样能把go写的稍微漂亮一点?
发信站: BBS 未名空间站 (Thu Apr 30 11:23:18 2015, 美东)
比如下边这段代码,怎么能好看一点?有什么trick吗?
func readUserCSV(file string, lambda func(string, string) error) error {
csvfile, err := os.Open(file)
if err {
return err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.Read()
_for:
for {
record, err := reader.Read()
switch err {
case nil:
if err ... 阅读全帖 |
|
G********7 发帖数: 234 | 2 要把在text里的数据转到csv格式, 程序如下。第四步(# Text file to CSV)出错。
出错提示是
“Traceback (most recent call last):
File "C:\yw\LatLon\txt2csv.py", line 24, in
txt = f.readline()
ValueError: Mixing iteration and read methods would lose data”
望赐教。多谢!
***********************************************
# Import Modules
import os,sys
# Variables
path = os.getcwd()
inputFile = os.path.join(path, "raw_files", "JAN 2013
FLVERTEXFLVertexFLVertex.txt")
CSVfile = os.path.join(path, "temp", "LatLon.csv")
print input... 阅读全帖 |
|
t***q 发帖数: 418 | 3 import os
import os.path
import StringIO
import csv
import datetime
dir = r"C:Python27"
if not os.path.exists(dir):
os.mkdir(dir)
my_list=[[1,2,3],[4,5,6]]
datestr=datetime.date.today().strftime("%y%m%d")
filename="good1_codes_{}".format(datestr)
with open(os.path.join(dir, filename+'.csv'), "w") as f:
csvfile=StringIO.StringIO()
csvwriter=csv.writer(csvfile)
for l in my_list:
csvwriter.writerow(l)
for a in csvfile.getvalue():
f.writelines(a)
以上程序,可以做这件事。试过了。 |
|
p*****2 发帖数: 21240 | 4 比如下边这段代码,怎么能好看一点?有什么trick吗?
func readUserCSV(file string, lambda func(string, string) error) error {
csvfile, err := os.Open(file)
if err {
return err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.Read()
_for:
for {
record, err := reader.Read()
switch err {
case nil:
if err = lambda(record[0], record[1]); err != nil {
return err
}
case io.EOF:
break _for
... 阅读全帖 |
|
d****n 发帖数: 1637 | 5 抛砖引玉
func readUserCSV(file string, lambda func(string, string) error) err error {
csvfile, err := os.Open(file)
if err {
return err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.Read()
for {
if err != nil{
if err == io.EOF{
err = nil
}
break
}
record, err := reader.Read()
if err !=nil{
continue
}
... 阅读全帖 |
|
d****n 发帖数: 1637 | 6 结尾没有return, at 9:17AM PCT
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
)
func main() {
if err := readUserCSV("./test.csv", lambda); err != nil {
//do something
}
}
func readUserCSV(file string, lambda func(string, string) error) error {
csvfile, err := os.Open(file)
if err != nil {
return err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
for {
record, err := reader.Read()
if err != nil {
... 阅读全帖 |
|
a***n 发帖数: 623 | 7 1 package main
2
3 import (
4 "encoding/csv"
5 "io"
6 "os"
7 )
8
9 func readUserCSV(file string, lambda func(string, string) error) error {
10 csvfile, err := os.Open(file)
11 if err != nil {
12 return err
13 }
14 defer csvfile.Close()
15
16 reader := csv.NewReader(csvfile)
17 reader.Read()
18
19 for {
20 record, err := reader.Read()
21 if err != nil &... 阅读全帖 |
|
l********a 发帖数: 1154 | 8 py3k和py2.x的文档来看就一个差别:
py3k:
"...If csvfile is a file object, it should be opened with newline=''.[1]"
[1] If newline='' is not specified, newlines embedded inside quoted fields
will not be interpreted correctly, and on platforms that use \r\n linendings
on write an extra \r will be added. It should always be safe to specify
newline='', since the csv module does its own (universal) newline handling.
py2.x:
"...If csvfile is a file object, it must be opened with the ‘b’ flag on
platforms where tha... 阅读全帖 |
|
s******y 发帖数: 352 | 9 %let dirpath=d:;
filename csvfile pipe "dir /b &dirpath.\*.csv";
data allcsv;
infile csvfile lrecl=1000;
input;
fname=catx('',"&dirpath.",_infile_);
infile dummy filevar=fname filename=myfile end=done firstobs=4
dsd truncover;
do while(not done);
input date :date9. Tier :$50. Ccy :$50.
Doc :$50. Sd1y :percent. Sd2y :percent.;
output;
end;
put 'Done with ' myfile=;
run;
external |
|