我正在尝试使用Python在StringIO对象中写入数据,然后最终使用psycopg2的copy_from()函数将此数据加载到postgres数据库中.
首先,当我这样做时,copy_from()抛出一个错误:错误:编码"UTF8"的无效字节序列:0xc92所以我遵循了这个问题.
我发现我的Postgres数据库有UTF8编码.
我正在编写数据的文件/ StringIO对象显示其编码如下:setgid非ISO扩展ASCII英文文本,带有很长的行,带有CRLF行终止符
我试图将我写入中间文件/ StringIO对象的每个字符串编码为UTF8格式.要做到这一点,每个字符串使用.encode(encoding ='UTF-8',errors ='strict')).
这是我现在得到的错误:UnicodeDecodeError:'ascii'编解码器无法解码位置47的字节0x92:序号不在范围内(128)
这是什么意思?我如何解决它?
编辑:我正在使用Python 2.7我的一些代码:
我从MySQL数据库中读取了根据MySQL Workbench以UTF-8编码的数据.这是用于将我的数据(从MySQL db获得)写入StringIO对象的几行代码:
# Populate the table_data variable with rows delimited by \n and columns delimited by \t
row_num=0
for row in cursor.fetchall() :
# Separate rows in a table by new line delimiter
if(row_num!=0):
table_data.write("\n")
col_num=0
for cell in row:
# Separate cells in a row by tab delimiter
if(col_num!=0):
table_data.write("\t")
table_data.write(cell.encode(encoding='UTF-8',errors='strict'))
col_num = col_num+1
row_num = row_num+1
Run Code Online (Sandbox Code Playgroud)
这是从我的StringIO对象table_data写入Postgres数据库的代码:
cursor = db_connection.cursor()
cursor.copy_from(table_data, <postgres_table_name>)
Run Code Online (Sandbox Code Playgroud) 我需要将数据从一些源数据源加载到Postgres数据库.要执行此任务,我首先将数据写入临时CSV文件,然后使用COPY FROM查询将数据从CSV文件加载到Postgres数据库.我在Python上做了所有这些.
代码如下所示:
table_name = 'products'
temp_file = "'C:\\Users\\username\\tempfile.csv'"
db_conn = psycopg2.connect(host, port, user, password, database)
cursor = db_conn.cursor()
query = """COPY """ + table_name + """ FROM """ + temp_file + " WITH NULL AS ''; """
cursor.execute(query)
Run Code Online (Sandbox Code Playgroud)
我想避免写入中间文件的步骤.相反,我想写一个Python对象,然后使用COPY FROM file方法将数据加载到postgres数据库.
我知道这种使用psycopg2的copy_from方法的技术,该方法将数据从StringIO对象复制到postgres数据库.但是,我不能使用psycopg2是有原因的,因此,我不希望我的COPY FROM任务依赖于库.我希望它是Postgres查询,也可以由任何其他postgres驱动程序运行.
请不要写入中间文件,建议更好的方法.
我在本地主机上的python上运行了一个简单的烧瓶Web应用程序.
Web应用程序在127.0.0.1:8000上运行.但我无法使用myHostComputerIPaddress:8000从我的网络中的远程计算机访问它.
我的主机是一台Windows机器.请指教.
一个简单的引导进度条,可在我的HTML代码中使用。
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" style="width:70%">
<span class="sr-only">70% Complete</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
无法在我的React组件中正确呈现。进度颜色不存在。
var DreamLane = React.createClass({
render: function(){
<div className="progress">
<div className="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" styles="width:70%">
<span className="sr-only">70% Complete</span>
</div>
</div>
}
});
Run Code Online (Sandbox Code Playgroud)
我该如何进行这项工作?
我有一个从用户输入收到的python字符串.
假设用户输入是这样的:
I am Enrolled in a course, 'MPhil' since 2014. I LOVE this 'SO MuCH'
Run Code Online (Sandbox Code Playgroud)
如果此字符串存储在名为input_string的变量中,并且我对其应用.lower(),则会将整个事物转换为小写.
input_string = input_string.lower()
Run Code Online (Sandbox Code Playgroud)
结果:
i am enrolled in a course, 'mphil' since 2014. i love this 'so much'
Run Code Online (Sandbox Code Playgroud)
这就是我希望小写的内容:将除引号之外的所有内容转换为小写.
i am enrolled in a course, 'MPhil' since 2014. i love this 'SO MuCH'
Run Code Online (Sandbox Code Playgroud) 我在amcharts.js中编写了一些代码来创建堆积条形图(以数据作为参数)和饼图.
代码可以在这里找到:http: //jsfiddle.net/akashdmukherjee/myd363tw/30/
HTML:
<script type="text/javascript" src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/serial.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/pie.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/themes/none.js"></script>
<div id="chartdiv"></div>
Run Code Online (Sandbox Code Playgroud)
CSS:
#chartdiv {
width : 100%;
height : 500px;
font-size : 11px;
}
Run Code Online (Sandbox Code Playgroud)
JS:
var year_wise_continent_breakdown_data =
[
{"year": 2003,"europe": 2.5,"namerica": 2.5,"asia": 2.1,"lamerica": 0.3,"meast":0.2,"africa": 0.1},
{"year": 2004,"europe": 2.6,"namerica": 2.7,"asia": 2.2,"lamerica": 0.3,"meast": 0.3,"africa": 0.1}
, {"year": 2005,"europe": 2.8,"namerica": 2.9,"asia": 2.4,"lamerica": 0.3,"meast": 0.3,"africa": 0.1}
];
var quarter_wise_continent_breakdown_data_for_2003 =
[
{"year": "Q1","europe": 0.5,"namerica": 2.1,"asia": 1.1,"lamerica": 0.3,"meast":0.2,"africa": 0.1},
{"year": "Q2","europe": 2.6,"namerica": 2.7,"asia": 3.2,"lamerica": 0.3,"meast": …Run Code Online (Sandbox Code Playgroud) python ×4
postgresql ×2
python-2.7 ×2
amcharts ×1
csv ×1
encoding ×1
flask ×1
javascript ×1
lowercase ×1
reactjs ×1
string ×1
utf ×1