Python UTF16流不以BOM开头

2024-10-03 19:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个比较两个csv文件的代码。 显示错误“UTF-16流不以BOM开头” 我尝试在PHP中添加BOM(csv是用PHP生成的),但没有成功。 尝试在Google和StackOverFlow上搜索,但没有找到这个问题的答案 请帮忙 Python代码:

import codecs
import io
import requests
import csv
import os
os.remove("small.csv")
file = input("File neve: ")

url = 'my site' + file + ".csv"
r = requests.get(url, allow_redirects=True)

with io.open("small.csv", "w", encoding="utf-8") as f:
    f.write(str(r.content.decode('unicode-escape')))
    f.close()

def readMyFile(filename):
    lat = []
    long = []
    date = []
    time = []
    allrow = []

    with codecs.open(filename, 'rU', 'utf-16') as csvDataFile:
        csvReader = csv.reader(csvDataFile,delimiter=';')
        for row in csvReader:
            allrow.append(row)
            lat.append(row[0])
            long.append(row[1])
            date.append(row[4])
            time.append(row[5])

    return lat, long, date, time, allrow
lat,long,date,time,allrow = readMyFile('small.csv')
lat1 = lat
long1 = long
date1 = date
time1 = time
allrow1 = allrow

lat,long,date,time,allrow = readMyFile('big.csv')
lat2 = lat
long2 = long
date2 = date
time2 = time
allrow2 = allrow
a = ''
sum1 = sum(1 for line in open('big.csv')) - 1

sum2 = sum(1 for line in open('small.csv')) - 1
time3 = 0
for i in range(sum1):
    lineno = 1
    for i2 in range(sum2):
        lat3 = float(lat1[lineno]) - float(lat2[i+1])
        long3 = float(long1[lineno]) - float(long2[i+1])
        #print(float(lat2[i+1]))
        time11 = time1[lineno].split(':')
        time12 = time2[i+1].split(':')
        if(lat3 < 0.0000899 and lat3 > -0.0000899 and long3 < 0.000123 and long3 > -0.000123 and date2[i+1] == date1[lineno] and time11[0] == time12[0] and int(time3) < 6 and int(time3) > -6):
            a = ""
            for c in range(len(allrow2[lineno])):
                b = list(allrow2[lineno])[c]
                b = b.replace(";", ",")
                a = a + b + ";"
            a = a + "\n"
            f = open("good.csv", "a")
            f.write(a)
            f.close()

        lineno = lineno + 1

Tags: andcsvinimportfordatetimeopen